C#.net模拟提交表单POST

方法一、
System.Net.WebClient WebClientObj        = new System.Net.WebClient();
   System.Collections.Specialized.NameValueCollection PostVars  = new System.Collections.Specialized.NameValueCollection();
   PostVars.Add("A1","0");
   PostVars.Add("A2","0");
   PostVars.Add("A3","000");

   try
   {
    byte[] byRemoteInfo   = WebClientObj.UploadValues("http://www.lovezhao.com/vote.asp","POST",PostVars);
    //下面都没用啦,就上面一句话就可以了
    string sRemoteInfo   = System.Text.Encoding.Default.GetString(byRemoteInfo);  
    //这是获取返回信息
    richTextBox_instr.Text  += sRemoteInfo;
   }
   catch
   {}



方法二、
string url = "网址";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string s = "要提交的数据";
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes (LoginInfo);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes,0,requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd(); Response.Write(line); sr.Close(); res.Close(); 

 

1. 创建httpWebRequest对象 
HttpWebRequest不能直接通过new来创建,只能通过WebRequest.Create(url)的方式来获得。 
WebRequest是获得一些列应用层协议对象的一个统一的入口(工厂模式),它根据参数的协议来确定最终创建的对象类型。所以我们的程序里面有一个对返回对象的类型进行测试的过程。 
2. 初始化HttpWebRequest对象 
这个过程提供一些http请求常用的属性:agentstring,contenttype等其中agentstring比较有意思,它是用来识别你用的浏览器名字的,通过设置这个属性你可以欺骗服务器你是一个IE,firefox甚至是mac里面的safari。很多认真设计的网站都会根据这个值来返回对用户浏览器特别优化过的代码。

3. 附加要POST给服务器的数据到HttpWebRequest对象 
附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。 
4. 读取服务器的返回信息 
读取服务器返回的时候,要注意返回数据的encoding。如果我们提供的解码类型不对会造成乱码。比较常见的是utf-8和gb2312之间的混淆,据我测试,国内的主机一般都是gb2312编码的。一般设计良好的网站会把它编码的方式放在返回的http header里面,但是也有不少网站根本没有,我们只能通过一个对返回二进制值的统计方法来确定它的编码方式。

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="method">方法</param>
        /// <param name="param">json参数</param>
        /// <returns></returns>
        public static string WebServiceApp(string url, string method, string param)
        {
            //转换输入参数的编码类型,获取bytep[]数组 
            byte[] byteArray = Encoding.UTF8.GetBytes("json=" + param);
            //初始化新的webRequst
            //1. 创建httpWebRequest对象
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url + "/" + method));
            //2. 初始化HttpWebRequest对象
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。)
            Stream newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            //4. 读取服务器的返回信息
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string phpend = php.ReadToEnd();
            return phpend;
           
        }
posted @ 2016-03-22 19:34  华丽》》转身  阅读(14422)  评论(0编辑  收藏  举报