Document

C#利用HttpWebRequest进行post请求的示例(HTTPS)

代码如下:

 

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.Security;  
  6. using System.Security.Cryptography.X509Certificates;  
  7. using System.Net;  
  8. using System.IO;  
  9. using System.IO.Compression;  
  10. using System.Text.RegularExpressions;     
  11.   
  12. namespace HttpWebRequestDemo  
  13. {  
  14.     class Program  
  15.     {  
  16.         private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";  
  17.   
  18.         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)  
  19.         {  
  20.             return true; //总是接受     
  21.         }  
  22.   
  23.         public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,Encoding charset)  
  24.         {  
  25.             HttpWebRequest request = null;  
  26.             //HTTPSQ请求  
  27.             ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
  28.             request = WebRequest.Create(url) as HttpWebRequest;  
  29.             request.ProtocolVersion = HttpVersion.Version10;  
  30.             request.Method = "POST";  
  31.             request.ContentType = "application/x-www-form-urlencoded";  
  32.             request.UserAgent = DefaultUserAgent;  
  33.             //如果需要POST数据     
  34.             if (!(parameters == null || parameters.Count == 0))  
  35.             {  
  36.                 StringBuilder buffer = new StringBuilder();  
  37.                 int i = 0;  
  38.                 foreach (string key in parameters.Keys)  
  39.                 {  
  40.                     if (i > 0)  
  41.                     {  
  42.                         buffer.AppendFormat("&{0}={1}", key, parameters[key]);  
  43.                     }  
  44.                     else  
  45.                     {  
  46.                         buffer.AppendFormat("{0}={1}", key, parameters[key]);  
  47.                     }  
  48.                     i++;  
  49.                 }  
  50.                 byte[] data = charset.GetBytes(buffer.ToString());  
  51.                 using (Stream stream = request.GetRequestStream())  
  52.                 {  
  53.                     stream.Write(data, 0, data.Length);  
  54.                 }  
  55.             }  
  56.             return request.GetResponse() as HttpWebResponse;  
  57.         }     
  58.   
  59.         static void Main(string[] args)  
  60.         {  
  61.             string url = "https://192.168.1.101/httpOrg/create";  
  62.             Encoding encoding = Encoding.GetEncoding("utf-8");  
  63.             IDictionary<string, string> parameters = new Dictionary<string, string>();  
  64.             parameters.Add("authuser", "*****");  
  65.             parameters.Add("authpass", "*****");  
  66.             parameters.Add("orgkey","*****");  
  67.             parameters.Add("orgname", "*****");  
  68.             HttpWebResponse response = Program.CreatePostHttpResponse(url,parameters,encoding);  
  69.             //打印返回值  
  70.             Stream stream = response.GetResponseStream();   //获取响应的字符串流  
  71.             StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
  72.             string html = sr.ReadToEnd();   //从头读到尾,放到字符串html  
  73.             Console.WriteLine(html);   
  74.         }  
  75.     }  
  76. }  

HttpWebRequest默认会用代理进行连接,导致获取结果比较慢。解决办法是,配置:

request.Proxy = null;

不使用代理,即可。

posted @ 2018-01-09 11:26  从未被超越  阅读(957)  评论(0编辑  收藏  举报