您的位置:首页 > 编程学习 > C# > 正文

HttpWebRequest的使用

更多 时间:2013-11-27 类别:编程学习 浏览量:5578

HttpWebRequest的使用

HttpWebRequest的使用

HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来对数据进行获取和提交。下面对这两种方式进行一下说明:

GET 方式

GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.studyofnet.com/?hl=zh-CN 中,前面部分 http://www.studyofnet.com表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。

程序代码如下:
 

  • C# 代码   复制
  • 
      HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com?hl=zh-CN" );
    req.Method = "GET";
    using (WebResponse wr = req.GetResponse())
    {
    //在这里对接收到的页面内容进行处理
    }
    
    		

  • 使用 GET 方式提交中文数据。

    GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种。

    用 gb2312 方式编码访问的程序代码如下:

  •  
  • C# 代码   复制
  • 
    Encoding myEncoding = Encoding.GetEncoding("gb2312");
    string address = "http://www.studyofnet.com/?" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
    req.Method = "GET";
    using (WebResponse wr = req.GetResponse())
    {
    //在这里对接收到的页面内容进行处理
    }
    
    		

  • 在上面的程序代码中,我们以 GET 方式访问了网址 http://www.studyofnet.com ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。

     

    POST 方式

    POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。

    程序代码如下:

  •  
  • C# 代码   复制
  • 
    string param = "hl=zh-CN&newwindow=1";
    byte[] bs = Encoding.ASCII.GetBytes(param);
    
    HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com/" );
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = bs.Length;
    
    using (Stream reqStream = req.GetRequestStream())
    {
       reqStream.Write(bs, 0, bs.Length);
    }
    using (WebResponse wr = req.GetResponse())
    {
       //在这里对接收到的页面内容进行处理
    } 
     
    
    		

  • 在上面的代码中,我们访问了 http://www.studyofnet.com 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。
     

    使用 POST 方式提交中文数据

    POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。

    用 gb2312 方式编码访问的程序代码如下:
     

  • C# 代码   复制
  • 
     Encoding myEncoding = Encoding.GetEncoding("gb2312");
    string param = HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding) + "&" + HttpUtility.UrlEncode("参数二", myEncoding) + "=" + HttpUtility.UrlEncode("值二", myEncoding);
    
    byte[] postBytes = Encoding.ASCII.GetBytes(param);
    
    HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com/" );
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
    req.ContentLength = postBytes.Length;
    
    using (Stream reqStream = req.GetRequestStream())
    {
       reqStream.Write(bs, 0, bs.Length);
    }
    using (WebResponse wr = req.GetResponse())
    {
       //在这里对接收到的页面内容进行处理
    } 
    
    		
  • 从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。
     

    用C#语言写的关于HttpWebRequest 类的使用方法

  •  
  • C# 代码   复制
  • 
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace HttpWeb
    {
        /// <summary> 
        ///  Http操作类 
        /// </summary> 
        public static class httptest
        {
            /// <summary> 
            ///  获取网址HTML 
            /// </summary> 
            /// <param name="URL">网址 </param> 
            /// <returns> </returns> 
            public static string GetHtml(string URL)
            {
                WebRequest wrt;
                wrt = WebRequest.Create(URL);
                wrt.Credentials = CredentialCache.DefaultCredentials;
                WebResponse wrp;
                wrp = wrt.GetResponse();
                string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
                try
                {
                    wrt.GetResponse().Close();
                }
                catch (WebException ex)
                {
                    throw ex;
                }
                return reader;
            }
            /// <summary> 
            /// 获取网站cookie 
            /// </summary> 
            /// <param name="URL">网址 </param> 
            /// <param name="cookie">cookie </param> 
            /// <returns> </returns> 
            public static string GetHtml(string URL, out string cookie)
            {
                WebRequest wrt;
                wrt = WebRequest.Create(URL);
                wrt.Credentials = CredentialCache.DefaultCredentials;
                WebResponse wrp;
    
                wrp = wrt.GetResponse();
    
    
                string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
    
                try
                {
                    wrt.GetResponse().Close();
                }
                catch (WebException ex)
                {
                    throw ex;
                }
    
                cookie = wrp.Headers.Get("Set-Cookie");
                return html;
            }
            public static string GetHtml(string URL, string postData, string cookie, out string header, string server)
            {
                return GetHtml(server, URL, postData, cookie, out header);
            }
            public static string GetHtml(string server, string URL, string postData, string cookie, out string header)
            {
                byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);
                return GetHtml(server, URL, byteRequest, cookie, out header);
            }
            public static string GetHtml(string server, string URL, byte[] byteRequest, string cookie, out string header)
            {
                byte[] bytes = GetHtmlByBytes(server, URL, byteRequest, cookie, out header);
                Stream getStream = new MemoryStream(bytes);
                StreamReader streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
                string getString = streamReader.ReadToEnd();
                streamReader.Close();
                getStream.Close();
                return getString;
            }
    
            /// <summary> 
            /// Post模式浏览 
            /// </summary> 
            /// <param name="server">服务器地址 </param> 
            /// <param name="URL">网址 </param> 
            /// <param name="byteRequest"></param> 
            /// <param name="cookie">cookie </param> 
            /// <param name="header">句柄 </param> 
            /// <returns> </returns> 
            public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
    
      
    标签:C# HttpWebRequest