WEBHttpRequest
2014-01-17 09:21
246 查看
public string Url { get; set; }
public string Refer { get; set; }
public string Method { get; set; }
public string PostData { get; set; }
object obj = new object();
public WEBHttpRequest() { }
public WEBHttpRequest(string loginUrl, string refer, string method, string postData)
{
Url = loginUrl;
Refer = refer;
Method = method;
PostData = postData;
}
public void SendAutoDataToServer(Action<string> callback, bool IsKeepAlive, bool IsAllowAutoRedirect)
{
lock (obj)
{
HttpWebRequest httpWebRequest;
byte[] byteArray = Encoding.ASCII.GetBytes(PostData);
//基于apache服务器,IIS发布的则不需要
ServicePointManager.Expect100Continue = false;
//创建对url的请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
httpWebRequest.Referer = Refer;
httpWebRequest.Accept = "*/*";
httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Headers["Accept-Language"] = "zh-cn";
httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0;)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.KeepAlive = IsKeepAlive;
httpWebRequest.Headers["Cache-Control"] = "no-cache";
httpWebRequest.AllowAutoRedirect = IsAllowAutoRedirect;
//协议方式
httpWebRequest.Method = Method;
httpWebRequest.Timeout = 20000;
//忽略证书
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};
//post开始
//请求内容长度
if (httpWebRequest.Method == "POST" || httpWebRequest.Method == "post")
{
try
{
httpWebRequest.ContentLength = byteArray.Length;
httpWebRequest.BeginGetRequestStream((a) =>
{
try
{
using (Stream datasStream = httpWebRequest.EndGetRequestStream(a))
{
datasStream.Write(byteArray, 0, byteArray.Length);
//异步返回html
httpWebRequest.BeginGetResponse((ar) =>
{
try
{
using (var webresponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar))
{
using (var stream = webresponse.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
callback(sr.ReadToEnd());
}
}
}
}
catch (Exception e) { callback(e.ToString()); }
finally { httpWebRequest.Abort(); }
}, null);
}
}
catch (Exception e) { callback(e.ToString()); return; }
}, null);
}
catch (Exception e) { callback(string.Empty); return; }
}
else
{
//异步返回html
httpWebRequest.BeginGetResponse((ar) =>
{
try
{
using (var webresponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar))
{
using (var stream = webresponse.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
callback(sr.ReadToEnd());
}
}
}
}
catch (Exception e) { callback(string.Empty); }
finally { httpWebRequest.Abort(); }
}, null);
}
}
}
public string Refer { get; set; }
public string Method { get; set; }
public string PostData { get; set; }
object obj = new object();
public WEBHttpRequest() { }
public WEBHttpRequest(string loginUrl, string refer, string method, string postData)
{
Url = loginUrl;
Refer = refer;
Method = method;
PostData = postData;
}
public void SendAutoDataToServer(Action<string> callback, bool IsKeepAlive, bool IsAllowAutoRedirect)
{
lock (obj)
{
HttpWebRequest httpWebRequest;
byte[] byteArray = Encoding.ASCII.GetBytes(PostData);
//基于apache服务器,IIS发布的则不需要
ServicePointManager.Expect100Continue = false;
//创建对url的请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
httpWebRequest.Referer = Refer;
httpWebRequest.Accept = "*/*";
httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Headers["Accept-Language"] = "zh-cn";
httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0;)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.KeepAlive = IsKeepAlive;
httpWebRequest.Headers["Cache-Control"] = "no-cache";
httpWebRequest.AllowAutoRedirect = IsAllowAutoRedirect;
//协议方式
httpWebRequest.Method = Method;
httpWebRequest.Timeout = 20000;
//忽略证书
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};
//post开始
//请求内容长度
if (httpWebRequest.Method == "POST" || httpWebRequest.Method == "post")
{
try
{
httpWebRequest.ContentLength = byteArray.Length;
httpWebRequest.BeginGetRequestStream((a) =>
{
try
{
using (Stream datasStream = httpWebRequest.EndGetRequestStream(a))
{
datasStream.Write(byteArray, 0, byteArray.Length);
//异步返回html
httpWebRequest.BeginGetResponse((ar) =>
{
try
{
using (var webresponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar))
{
using (var stream = webresponse.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
callback(sr.ReadToEnd());
}
}
}
}
catch (Exception e) { callback(e.ToString()); }
finally { httpWebRequest.Abort(); }
}, null);
}
}
catch (Exception e) { callback(e.ToString()); return; }
}, null);
}
catch (Exception e) { callback(string.Empty); return; }
}
else
{
//异步返回html
httpWebRequest.BeginGetResponse((ar) =>
{
try
{
using (var webresponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar))
{
using (var stream = webresponse.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
callback(sr.ReadToEnd());
}
}
}
}
catch (Exception e) { callback(string.Empty); }
finally { httpWebRequest.Abort(); }
}, null);
}
}
}
相关文章推荐
- c#HttpWebRequest修改cookie
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- 使用HttpWebRequest向网站模拟上传数据
- HttpWebRequest模拟浏览器的GET、POST请求
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- [WebService] 使用httpWebrequest 调用并调试WebService
- HttpWebRequest.GetResponse出异常时的解决办法
- AJAX和XmlHttpRequest下的WEB开发
- WebClient vs HttpClient vs HttpWebRequest
- HttpWebRequest抓数据遇到的问题
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- c#中HttpWebRequest对应302重定向为什么取不到 cookie
- HttpWebRequest 二三事
- C# 通过HttpWebRequest在后台对WebService进行调用
- System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜在危险的 Request.Form 值。
- C#利用HttpWebRequest,Stream,HttpWebResponse,StreamReader获取post返回的数据
- HttpWebRequest的GetRespons偶尔超时、死掉的原因及解决办法
- HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
- HttpWebRequest 获取页面
- 使用HttpWebRequest远程下载文件