后台post get请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后台post get请求相关的知识,希望对你有一定的参考价值。

/// <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请求的主要内容,如果未能解决你的问题,请参考以下文章

使用建造者模式创建模拟数据

Java设计模式--------建造者模式(Builder模式)

设计模式--建造者模式

设计模式之Builder(建造者)(转)

Java设计模式百例 - 建造者模式

设计模式之建造者模式(builder)