public static string CreateRequest(string reqUrl, string method, bool useProxy, Encoding code, string param)
{
string resultStr = string.Empty;
//* * * * * * * * * * * * * * * * * * * * 创建请求 * * * * * * * * * * * * * * * * * * * *
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(reqUrl);
//* * * * * * * * * * * * * * * * * * * * 设置参数 * * * * * * * * * * * * * * * * * * * *
webReq.Method = method;
webReq.KeepAlive = true;
webReq.Timeout = 2000;
webReq.ContentType = "text/html";
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.4; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
//* * * * * * * * * * * * * * * * * * * * 判定是否需要代理 * * * * * * * * * * * * * * * * * * * *
if (useProxy) webReq.Proxy = new WebProxy("10.19.110.32", 8080);
//* * * * * * * * * * * * * * * * * * * * post方式传递参数 * * * * * * * * * * * * * * * * * * * *
if (method == "POST")
{
byte[] bs = Encoding.ASCII.GetBytes(param);
webReq.ContentLength = bs.Length;
using (Stream reqStream = webReq.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
}
//* * * * * * * * * * * * * * * * * * * * 获取 response * * * * * * * * * * * * * * * * * * * *
using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), code))
{
resultStr = reader.ReadToEnd();
}
if (response != null) response.Close();
}
//* * * * * * * * * * * * * * * * * * * * 完成请求,输出返回信息 * * * * * * * * * * * * * * * * * * * *
return resultStr;
}
09-25