public enum HttpVerb
{
Get,
Post
}
public class HttpHelper
{
private string _contentType = "application/x-www-form-urlencoded";
//private string _contentType = "application/json";
//private string _contentType = "text/xml";
private int _timeout = ;
public string EndPoint { get; set; }
public HttpVerb Method { get; set; }
public string ContentType { get { return _contentType; } set { _contentType = value; } }
public string PostData { get; set; }
/// <summary>
/// 默认15000毫秒
/// </summary>
public int Timeout { get { return _timeout; } set { _timeout = value; } }
public HttpHelper(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.Get;
ContentType = _contentType;
PostData = "{}";
}
public string Post(string data)
{
var service = new Uri(EndPoint);
var request = WebRequest.CreateHttp(service);
request.Method = "POST";
request.Timeout = Timeout;
request.KeepAlive = true;
request.ContentLength = ;
request.ContentType = ContentType;
request.Accept = "application/json";
var bytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
/// <summary>
/// Request server
/// </summary>
/// <param name="parameters">eg. "?q=1234"</param>
/// <returns></returns>
public string Get(string parameters)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.CreateHttp(service);
request.Method = "GET";
request.Timeout = Timeout;
request.KeepAlive = true;
request.ContentLength = ;
request.ContentType = ContentType;
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
/// <summary>
/// 下载更新二进制文件
/// </summary>
/// <param name="parameters">eg. "?q=1234"</param>
/// <returns></returns>
public byte[] DownLoad(string parameters)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.CreateHttp(service);
request.Timeout = Timeout;
request.KeepAlive = true;
request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture);
request.ContentLength = ;
request.ContentType = ContentType;
if (Method == HttpVerb.Post)
{
var bytes = Encoding.UTF8.GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var result = new MemoryStream())
{
const int bufferLen = ;
var buffer = new byte[bufferLen];
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
var len = responseStream.Read(buffer, , bufferLen);
while (len > )
{
result.Write(buffer, , len);
len = responseStream.Read(buffer, , bufferLen);
}
}
}
return result.ToArray();
}
}
}
}