码迷,mamicode.com
首页 > 其他好文 > 详细

后台post get请求

时间:2016-02-26 11:47:03      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

/// <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.UserAgent = "59MiaoApp";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

byte[] postData = Encoding.UTF8.GetBytes(BuildPostData(parameters));
Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();

HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}

/// <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.UserAgent = "Top4Net";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";


HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}

后台post get请求

标签:

原文地址:http://www.cnblogs.com/-maomao/p/5219522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!