using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace DotNet.Utilities
{
#region 调用试例
// static void Main(string[] args)
// {
// string url = "www.baidu.com";
// string param = "";
// string method="GET";
// for (int i = 0; i < 1; i++)//发送多条请求看看会不会有问题。
// {
// var result = HttpUtility.Request(method, url, param, onComplete);
// Console.WriteLine("-------------Result----------------");
// Console.WriteLine(result);
// Console.WriteLine("-------------End----------------");
// }
// Console.ReadLine();
// }
// ///
// /// 请求完成后要执行的操作
// ///
// /// Http状态码
// /// 返回结果
// public static void onComplete(HttpStatusCode code, string result)
// {
// Console.WriteLine("HttpRequest complete! as follow:");
// Console.WriteLine("HttpStatusCode:" + code);
// Console.WriteLine("HttpRequestResult:" + result);
// }
#endregion
///
/// Http请求
///
public class HttpUtility
{
///
///通用HttpWebRequest
///
/// 请求方式(POST/GET)
/// 请求地址
/// 请求参数
/// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
/// 请求返回的结果
public static string Request(string method, string url, string param, Action onComplete = null)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException("URL");
switch (method.ToUpper())
{
case "POST":
return Post(url, param, onComplete);
case "GET":
return Get(url, param, onComplete);
default:
throw new EntryPointNotFoundException("method:" + method);
}
}
///
/// Post请求
///
/// 请求地址
/// 参数
/// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
/// 请求返回的结果
public static string Post(string url, string param, Action onComplete = null)
{
UrlCheck(ref url);
byte[] bufferBytes = Encoding.UTF8.GetBytes(param);
var request = WebRequest.Create(url) as HttpWebRequest;//HttpWebRequest方法继承自WebRequest, Create方法在WebRequest中定义,因此此处要显示的转换
request.Method = "POST";
request.ContentLength = bufferBytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
try
{
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bufferBytes, 0, bufferBytes.Length);
}
}
catch (WebException ex)
{
return ex.Message;
}
return HttpRequest(request, onComplete);
}
///
/// GET请求
///
/// 请求地址
/// 参数
/// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
/// 请求返回结果
public static string Get(string url, string param, Action onComplete = null)
{
UrlCheck(ref url);
if (!string.IsNullOrEmpty(param))
if (!param.StartsWith("?"))
param += "?" + param;
else
param += param;
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
return HttpRequest(request, onComplete);
}
///
/// 请求的主体部分(由此完成请求并返回请求结果)
///
/// 请求的对象
/// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
/// 请求返回结果
private static string HttpRequest(HttpWebRequest request, Action onComplete = null)
{
HttpWebResponse response = null;
try
{
response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
if (response == null)
{
if (onComplete != null)
onComplete(HttpStatusCode.NotFound, "请求远程返回为空");
return null;
}
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
if (onComplete != null)
onComplete(response.StatusCode, result);
return result;
}
///
/// URL拼写完整性检查
///
/// 待检查的URL
private static void UrlCheck(ref string url)
{
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
url = "http://" + url;
}
}
}