标签:
/// <summary>
/// 执行HTTP POST请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="parameters">请求参数</param>
/// <returns>HTTP响应</returns>
public static string DoPost(string url, IDictionary<string, string> parameters)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
// Encoding encoding1 = Encoding.GetEncoding("gb2312");
//byte[] postData = encoding1.GetBytes(BuildPostData(parameters));
byte[] postData = Encoding.UTF8.GetBytes(BuildPostData(parameters));
int sendCount = 0;
while (sendCount != 5)
{
try
{
Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
string en = rsp.CharacterSet;//获取页面编码
if (en == null || en.Length == 0) en = "gb2312";
if (en.ToLower() == "iso-8859-1") en = "gb2312";
Encoding encoding = Encoding.GetEncoding(en);
return GetResponseAsString(rsp, encoding);
}
catch
{
sendCount++;
}
}
return "远程服务器返回错误或者操作超时";
}
/// <summary>
/// 执行HTTP GET请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="parameters">请求参数</param>
/// <returns>HTTP响应</returns>
public static string DoGet(string url, IDictionary<string, string> parameters)
{
if (parameters != null && parameters.Count > 0)
{
if (url.Contains("?"))
{
url = url + "&" + BuildPostData(parameters);
}
else
{
url = url + "?" + BuildPostData(parameters);
}
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
int sendCount = 0;
while (sendCount != 5)
{
try
{
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
string en = rsp.CharacterSet;//获取页面编码
if (en == null || en.Length == 0) en = "utf-32";
if (en.ToLower() == "iso-8859-1") en = "gb2312";
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}
catch
{
sendCount++;
}
}
return "远程服务器返回错误或者操作超时";
}
/// <summary>
/// 组装普通文本请求参数。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
private static string BuildPostData(IDictionary<string, string> parameters)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
//postData.Append(Uri.EscapeDataString(value));
postData.Append(value);
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 把响应流转换为文本。
/// </summary>
/// <param name="rsp">响应流对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>响应文本</returns>
private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
StringBuilder result = new StringBuilder();
Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding);
// 每次读取不大于512个字符,并写入字符串
char[] buffer = new char[512];
int readBytes = 0;
while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
{
result.Append(buffer, 0, readBytes);
}
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
return result.ToString();
}
标签:
原文地址:http://www.cnblogs.com/Leo_wl/p/5287490.html