HttpWebRequest 完成Get和Post
2011-09-13 14:28 LoujaDy 阅读(475) 评论(0) 编辑 收藏 举报 /// <summary>
/// Get方式请求
/// </summary>
/// <param name="getUrl">请求的URL</param>
/// <param name="cookies">请求时带上的Cookies</param>
/// <param name="timeOut">请求的超时时间</param>
/// <param name="userAgent">请求用户的Agent</param>
/// <returns></returns>
public static HttpWebResponse HttpGetResponse(string getUrl,int timeOut,string userAgent,string cookiestr)
{
//检查URL
if (string.IsNullOrEmpty(getUrl))
{
throw new ArgumentException("请求的URL不能为空!");
}
//创建Request
HttpWebRequest request = WebRequest.Create(getUrl) as HttpWebRequest;
//请求方式为Get
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
//用户的UserAgent
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
//如果有超时设定
if (timeOut > 0)
{
request.Timeout = timeOut;
}
//设置请求带上的Cookies
if (!string.IsNullOrEmpty(cookiestr))
{
request.Headers["Cookie"] = cookiestr;
}
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// Post方式请求
/// </summary>
/// <param name="postUrl">post的URL</param>
/// <param name="parameters">post过去的参数</param>
/// <param name="timeout">请求超时时间</param>
/// <param name="userAgent">请的用户的agent</param>
/// <param name="requestEncoding">请求的编码</param>
/// <param name="cookiestr">请求要带上的cookie</param>
/// <returns></returns>
public static HttpWebResponse HttpPostResponse(string postUrl, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding,string cookiestr)
{
if (string.IsNullOrEmpty(postUrl))
{
throw new ArgumentNullException("postUrl");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (postUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((s, x, xc, e) => { return true; });
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version11;
}
else
{
request = WebRequest.Create(postUrl) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (!string.IsNullOrEmpty(cookiestr))
{
request.Headers["Cookie"] = cookiestr;
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse hr= request.GetResponse() as HttpWebResponse;
return hr;
}
/// Get方式请求
/// </summary>
/// <param name="getUrl">请求的URL</param>
/// <param name="cookies">请求时带上的Cookies</param>
/// <param name="timeOut">请求的超时时间</param>
/// <param name="userAgent">请求用户的Agent</param>
/// <returns></returns>
public static HttpWebResponse HttpGetResponse(string getUrl,int timeOut,string userAgent,string cookiestr)
{
//检查URL
if (string.IsNullOrEmpty(getUrl))
{
throw new ArgumentException("请求的URL不能为空!");
}
//创建Request
HttpWebRequest request = WebRequest.Create(getUrl) as HttpWebRequest;
//请求方式为Get
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
//用户的UserAgent
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
//如果有超时设定
if (timeOut > 0)
{
request.Timeout = timeOut;
}
//设置请求带上的Cookies
if (!string.IsNullOrEmpty(cookiestr))
{
request.Headers["Cookie"] = cookiestr;
}
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// Post方式请求
/// </summary>
/// <param name="postUrl">post的URL</param>
/// <param name="parameters">post过去的参数</param>
/// <param name="timeout">请求超时时间</param>
/// <param name="userAgent">请的用户的agent</param>
/// <param name="requestEncoding">请求的编码</param>
/// <param name="cookiestr">请求要带上的cookie</param>
/// <returns></returns>
public static HttpWebResponse HttpPostResponse(string postUrl, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding,string cookiestr)
{
if (string.IsNullOrEmpty(postUrl))
{
throw new ArgumentNullException("postUrl");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (postUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((s, x, xc, e) => { return true; });
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version11;
}
else
{
request = WebRequest.Create(postUrl) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (!string.IsNullOrEmpty(cookiestr))
{
request.Headers["Cookie"] = cookiestr;
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse hr= request.GetResponse() as HttpWebResponse;
return hr;
}
作者:Louja
出处:http://loujady.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面给出原文连接,否则保留追究法律责任的权利。