public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, int> parameters, string userAgent, Encoding requestEncoding)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "DELETE";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("ContentType", "text/xml; charset=utf-8");
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
//else
//{
// request.UserAgent = DefaultUserAgent;
//}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
var buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
相关文章
- 用python发送GET和POST请求
- 微信通过openID发送消息/后台post、get提交并接收数据
- php发送get、post请求的几种方法
- 自定义volley框架StringRequest类 ——个方法发送各种请求(get post 参数 cookie)
- java 发送get,post请求
- php 利用socket发送HTTP请求(GET,POST)
- vue 使用 axios 时 post 请求方法传参无法发送至后台
- curl模拟delete/put/post/get请求
- php使用fsockopen函数发送post,get请求获取网页内容的方法
- jquery.ajax post/get/delete/put 请求方法封装