C#发起http及https请求

ershen290 贡献于2013-11-22

作者 上善若水  创建于2009-12-29 14:36:00   修改者上善若水  修改于2009-12-30 03:29:00字数4698

文档摘要:推荐一个http/web开发的工具HTTP Analyzer在。web/http开发中,需要了解http通讯的具体内容,这个很不少工具不过大部分都是和浏览器绑定的,如firefox下的firebug,ie下的HttpWatch或者HttpLiveHeader之类的和浏览器绑定有很多不爽的地方,比方说性能问题。另外就是不通用,只能对绑定的浏览器起作用,不是对任意程序。是的,这里介绍的这个工具HTTPAnalyzer,可以对任意程序的Http请求进行监控!
关键词:

1 推荐一个http/web开发的工具 HTTP Analyzer 在web/http开发中,需要了解http通讯的具体内容,这个很不少工具 不过大部分都是和浏览器绑定的,如firefox下的firebug,ie下的HttpWatch或者 Http Live Header之类的 和浏览器绑定有很多不爽的地方,比方说性能问题。另外就是不通用,只能对绑定的浏览器起作用,不是对任意程序。 是的,这里介绍的这个工具 HTTP Analyzer,可以对任意程序的Http请求进行监控! 具体介绍自己搜索吧。这里提供,带破解,不是最新的版本,最新的自己搜吧。 2 C# WebRequest处理Https请求之使用客户端证书 我们使用https是,一般都只是服务器提供证书,用于验证服务器的身份 其实ssl是双向的,https有时也需要客户端提供证书,不少对安全性要求高的场合如银行业务就需要。 代码片段示例: HttpWebRequest request; //blabla X509Certificate cer = X509Certificate.CreateFromCertFile(“你的cer证书文件”); request.ClientCertificates.Add(cer); //blabla 3 C# WebRequest处理Https请求 正常情况下,处理https和http没有什么区别,如以下代码,显示了https://www.softlayer.com/的内容 string url = “https://www.softlayer.com/”; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) {     encoding = "UTF-8"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); Console.Write(reader.ReadToEnd()); response.Close(); 和http的不同就是地址由http改为了https 但是,如果你将 https://www.softlayer.com/ 改为 https://softlayer.com/,会发现一个异常 未能为 SSL/TLS 安全通道建立信任关系 怎么回事? 用浏览器试一下,访问 https://www.softlayer.com/ 没有问题 访问 https://softlayer.com/ 会有证书问题(虽然是我们自己造成的) 这就对了,如果有证书问题,不做特殊处理,将会抛出异常 怎么处理呢? 如果是dot net 2.0 或以上,加上一个确认函数就可以了,代码如: ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); string url = “https://softlayer.com/”; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) {     encoding = "UTF-8"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); Console.Write(reader.ReadToEnd()); response.Close(); public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // Always accept     Console.WriteLine(“accept ” + certificate.GetName());     return true; //总是接受 } using System.Net; using System.IO; using System.Web; using System.Net.Security; using System.Security; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; 如果是dot net 1.1,写法略有不同 ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); internal class AcceptAllCertificatePolicy : ICertificatePolicy {     public AcceptAllCertificatePolicy() {     }     public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wRequest, int certProb) {     // Always accept     return true;     } } 4 C# WebRequest发起Http Post请求模拟登陆并cookie处理示例 代码: CookieContainer cc=new CookieContainer(); string url = “http://mailbeta.263.net/xmweb”; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; request.CookieContainer=cc; string user=”user”; //用户名 string pass=”pass”; //密码 string data = “func=login&usr=” + HttpUtility.UrlEncode(user) + “&sel_domain=263.net&domain=263.net&pass=” + HttpUtility.UrlEncode(pass) + “&image2.x=0&image2.y=0&verifypcookie=&verifypip=”; request.ContentLength = data.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII); writer.Write(data); writer.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) {     encoding = "UTF-8"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); data = reader.ReadToEnd(); Console.WriteLine(data); response.Close(); int index=data.IndexOf("sid="); string sid=data.Substring(index+4,data.IndexOf("&",index)-index-4); Console.WriteLine(sid); url = "http://wm11.263.net/xmweb?func=mlst&act=show&usr="+user+"&sid="+sid+"&fid=1&desc=1&pg=1&searchword=&searchtype=&searchsub=&searchfd=&sort=4"; request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cc; foreach(Cookie cookie in response.Cookies) {     cc.Add(cookie); } response = (HttpWebResponse)request.GetResponse(); encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) {     encoding = "UTF-8"; //默认编码 } reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); data = reader.ReadToEnd(); Console.WriteLine(data); response.Close(); 这段代码的意思是,模拟登陆263邮箱,别列出收件箱内容(html代码) 5 c#使用http发起请求示例 c#例子代码如下: string url=”http://www.google.cn/”; HttpWebRequest request=(HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) {    encoding = "GB18030"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.GetEncoding(encoding)); Console.Write(reader.ReadToEnd()); response.Close(); 代码将输出google首页的html

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档