public class HttpHelper
{
#region Get Post Put Delete 请求
///
/// 返回指定Url的页面源码,Get提交
///
/// Url地址
/// 编码格式 gb2312 utf-8,默认 utf-8
///
public static string HttpGet(string url, string encoding = "utf-8")
{
return HttpRequest(url, "", "get", encoding);
}
///
/// Post提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpPost(string url, string body, string encoding = "utf-8")
{
return HttpRequest(url, body, "post", encoding);
}
///
/// Post提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpPost(string url, string encoding = "utf-8")
{
return HttpRequest(url, "", "post", encoding);
}
///
/// Delete提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpDelete(string url, string body, string encoding = "utf-8")
{
return HttpRequest(url, body, "delete", encoding);
}
///
/// Delete提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpDelete(string url, string encoding = "utf-8")
{
return HttpRequest(url, "", "delete", encoding);
}
///
/// Delete提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpPut(string url, string body, string encoding = "utf-8")
{
return HttpRequest(url, body, "put", encoding);
}
///
/// Delete提交
///
/// url地址
/// 内容
/// 编码 默认 utf-8
///
public static string HttpPut(string url, string encoding = "utf-8")
{
return HttpRequest(url, "", "put", encoding);
}
///
/// 模拟Http请求
///
/// 请求地址 http://www.baidu.com/
/// 参数:id=123@name=admin
/// 请求形式 post get delete put
/// 请求的页面编码 uft-8,gb2312,GBK
///
public static string HttpRequest(string httpUrl, string body, string method, string encoding = "utf-8")
{
//创建httpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
request.Method = method.ToUpper();
//填充要 post/get 的内容
if (!string.IsNullOrEmpty(body))
{
byte[] data = Encoding.GetEncoding(encoding).GetBytes(body);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
//发送 post/get 请求到服务器并读取服务器返回信息
Stream responseStream = null;
try
{
responseStream = request.GetResponse().GetResponseStream();
}
catch (Exception)
{
}
//读取服务器返回信息
string result = "";
if (responseStream != null)
{
StreamReader sr = null;
//读取
try
{
sr = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
result = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
}
return result;
}
#endregion
#region 提交获取状态码
///
/// 获取请求地址状态码
///
///
///
public static int GetStatusCode(string url, string host, out int time)
{
Stopwatch sw = new Stopwatch();
sw.Start();
int code = HttpGetStatusCode(url, host);
sw.Stop();
time = Convert.ToInt32(sw.ElapsedMilliseconds);
return code;
}
///
/// 获取请求地址状态码
///
///
///
public static int GetStatusCode(string url, string host)
{
int code = HttpGetStatusCode(url, host);
return code;
}
///
/// Get提交 获取状态码
///
///
///
///
///
public static int HttpGetStatusCode(string url, string host, string encoding = "utf-8")
{
return getStatusCode(url, "", host, "get", encoding);
}
///
/// Delete提交 获取状态码
///
///
///
///
///
public static int HttpPutStatusCode(string url, string body, string host, string encoding = "utf-8")
{
return getStatusCode(url, body, host, "put", encoding);
}
private static int getStatusCode(string url, string body, string host, string Method, string encoding = "utf-8")
{
//创建httpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 1 * 60 * 1000;//1分钟
if (!string.IsNullOrEmpty(host))
{
request.Host = host;
}
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
request.Method = Method.ToUpper();
//填充要 post/get 的内容
if (!string.IsNullOrEmpty(body))
{
byte[] data = Encoding.GetEncoding(encoding).GetBytes(body);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
try
{
return (int)((System.Net.HttpWebResponse)request.GetResponse()).StatusCode;
}
catch (Exception ex)
{
Log4Helper.WriteLog("获取状态码异常:", ex);
}
return -1;
}
#endregion
}