HttpWebRequest和HttpWebResponse

  

申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice或者json解析的方式,直接调用标准解析就可以。没想到客户用“请求响应模式”,直接访问人家的接口地址取数据,无奈打造一个通用的访问类吧

参考MSDN的解释:

HttpWebRequest类:提供WebRequest类的Http特定的实现。

HttpWebRequest类对WebRequest中定义的属性和方法提供支持,也对使用户能够直接与使用Http的服务器交互的附加属性和方法提供支持。

不要使用构造函数创建HttpWebRequest实例,请使用System.Net.WebRequest。创建(URI uriString)来创建实例,如果是URI Http://或Https://,
返回的是HttpWebRequest对象。(建立请求特定URI的对象)
当向资源发送数据时,GetRequestStream方法返回用于发送数据的流对象。(获取请求数据的流对象)
GetResponse方法向RequestUri属性指定的资源发出同步请求并返回包含该响应的HttpWebResponse。(获取来自互联网的响应)

好了,直接上代码:


使用System.Net;   使用先;   使用System.Security.Cryptography.X509Certificates;   使用System.Net.Security;   使用System.Windows.Forms;/*   *申公   *日期:   *说明:此类提供http POST和GET访问远程接口   * */名称空间ZJS.EDI.Business.HttpUtility   {///& lt; summary>///有关HTTP请求的辅助类///& lt;/summary>   公开课HttpWebResponseUtility   {      私有静态只读的字符串DefaultUserAgent=" Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.2;SV1;net CLR 1.1.4322;net CLR 2.0.50727)”;//浏览器   私有静态编码requestEncoding=System.Text.Encoding.UTF8;//字符集///& lt; summary>///创建得到方式的HTTP请求///& lt;/summary>///& lt;参数name=" url "祝辞请求的URL///& lt;参数name=俺薄弊4乔肭蟮某笔奔? lt;/param>///& lt;参数name=" userAgent祝辞请求的客户端浏览器信息,可以为空& lt;/param>///& lt;参数name=癈ookie”祝辞随同HTTP请求发送的饼干信息,如果不需要身份验证可以为空& lt;/param>///& lt; returns> & lt;/returns>   公共静态HttpWebResponse CreateGetHttpResponse(字符串url, int ?超时,userAgent字符串,CookieCollection饼干)   {   如果(string.IsNullOrEmpty (url))   {   把新ArgumentNullException (“url”);   }   作为HttpWebRequest HttpWebRequest请求=WebRequest.Create (url);   请求。方法=盎竦谩?   请求。UserAgent=DefaultUserAgent;   如果(! string.IsNullOrEmpty (userAgent))   {   请求。UserAgent=UserAgent;   }   如果(timeout.HasValue)   {   请求。超时=timeout.Value;   }   如果(饼干!=null)   {   请求。CookieContainer=new CookieContainer ();   request.CookieContainer.Add(饼干);   }   返回request.GetResponse HttpWebResponse ();   }///& lt; summary>///创建发布方式的HTTP请求///& lt;/summary>///& lt;参数name=" url "祝辞请求的URL///& lt;参数name=安问痹谒嫱肭筇拥牟问萍安问底值? lt;/param>///& lt;参数name=癈ookie”祝辞随同HTTP请求发送的饼干信息,如果不需要身份验证可以为空& lt;/param>///& lt; returns> & lt;/returns>   公共静态HttpWebResponse CreatePostHttpResponse(字符串url字符串参数,CookieCollection饼干)   {   如果(string.IsNullOrEmpty (url))   {   把新ArgumentNullException (“url”);   }      HttpWebRequest请求=零;   流流=零;//用于传参数的流      试一试   {//如果是发送HTTPS请求   如果(url)。StartsWith (“https”, StringComparison.OrdinalIgnoreCase))   {   ServicePointManager。ServerCertificateValidationCallback=new RemoteCertificateValidationCallback (CheckValidationResult);   请求=WebRequest.Create HttpWebRequest (url);//创建证书文件   System.Security.Cryptography.X509Certificates。X509Certificate objx509=new System.Security.Cryptography.X509Certificates.X509Certificate(应用程序。StartupPath + @“\ \ licensefile \ zjs.cer”);//添加到请求里   request.ClientCertificates.Add (objx509);   请求。ProtocolVersion=HttpVersion.Version10;   }   其他的   {   请求=WebRequest.Create HttpWebRequest (url);   }      请求。方法=癙OST”;//传输方式   请求。ContentType="应用程序/x-www-form-urlencoded”;//协议   请求。UserAgent=DefaultUserAgent;//请求的客户端浏览器信息,默认IE   请求。超时=6000;//超时时间,写死6秒//随同HTTP请求发送的饼干信息,如果不需要身份验证可以为空   如果(饼干!=null)   {   请求。CookieContainer=new CookieContainer ();   request.CookieContainer.Add(饼干);   }//如果需求的帖子传数据,转换成utf - 8编码   byte [] data=https://www.yisu.com/zixun/requestEncoding.GetBytes(参数);   请求。ContentLength=data.Length;      流=request.GetRequestStream ();   流。写(数据、0 data.Length);      stream.Close ();   }   捕获(异常ee)   {//写日志//LogHelper。   }   最后   {   如果(流!=null)   {   stream.Close ();   }   }      返回request.GetResponse HttpWebResponse ();   }//验证服务器证书回调自动验证   私有静态bool CheckValidationResult(对象发送方,X509Certificate证书,X509Chain链,SslPolicyErrors错误)   {   返回true;//总是接受   }>///

HttpWebRequest和HttpWebResponse