阅读数:59595
代码如下:
- using System;
- using Sy;
- using Sy;
- using Sy;
- using Sy;
- using Sy;
- using Sy;
- using Sy;
- using Sy.Compression;
- using Sy.RegularExpressions;
- namespace HttpWebrequestDemo
- {
- class Program
- {
- private static readonly string DefaultUserAgent = "Mozilla (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
- private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true; //总是接受
- }
- public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,Encoding charset)
- {
- HttpWebRequest request = null;
- //HTTPSQ请求
- ServicePoin = new RemoteCertificateValidationCallback(CheckValidationResult);
- request = WebReque(url) as HttpWebRequest;
- reque = H;
- reque = "POST";
- reque = "application/x-www-form-urlencoded";
- reque = DefaultUserAgent;
- //如果需要POST数据
- if (!(parameters == null || == 0))
- {
- StringBuilder buffer = new StringBuilder();
- int i = 0;
- foreach (string key in )
- {
- if (i > 0)
- {
- bu("&{0}={1}", key, parameters[key]);
- }
- else
- {
- bu("{0}={1}", key, parameters[key]);
- }
- i++;
- }
- byte[] data = c());
- using (Stream stream = reque())
- {
- (data, 0, da);
- }
- }
- return reque() as HttpWebResponse;
- }
- static void Main(string[] args)
- {
- string url = ";;
- Encoding encoding = Encoding.GetEncoding("utf-8");
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- ("authuser", "*****");
- ("authpass", "*****");
- ("orgkey","*****");
- ("orgname", "*****");
- HttpWebResponse response = Program.CreatePostHttpResponse(url,parameters,encoding);
- //打印返回值
- Stream stream = re(); //获取响应的字符串流
- StreamReader sr = new StreamReader(stream); //创建一个stream读取流
- string html = (); //从头读到尾,放到字符串html
- Con(html);
- }
- }
- }
HttpWebRequest默认会用代理进行连接,导致获取结果比较慢。解决办法是,配置:
reque = null;
不使用代理,即可。