public static class HttpRequestPost
{
/// <summary>
/// 向指定的URL地址发起一个POST请求,同时可以上传一些数据项。
/// </summary>
/// <param name="url">要请求的URL地址</param>
/// <param name="keyvalues">要上传的数据项</param>
/// <param name="encoding">发送,接收的字符编码方式</param>
/// <returns>服务器的返回结果</returns>
public static string SendHttpRequestPost(string url, Dictionary<string, string> keyvalues, Encoding encoding)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
//name1=value&name2=value
if (encoding == null)
{
encoding = Encoding.UTF8;
}
var mm = string.Join("&", (from m in keyvalues let keys = m.Key + "=" + HttpUtility.UrlEncode(m.Value) select keys).ToArray());
HttpWebRequest web = (HttpWebRequest)WebRequest.Create(url);
web.Method = "POST";
web.ContentType = "application/x-www-form-urlencoded";
byte[] data = encoding.GetBytes(mm);
StreamWriter writer = new StreamWriter(web.GetRequestStream(), encoding);
writer.Write(data);
using (WebResponse reon = web.GetResponse())
{
using (StreamReader read = new StreamReader(reon.GetResponseStream(), encoding))
{
return read.ReadToEnd();
}
}
}
/// <summary>
/// 向指定的URL地址发起一个POST请求,同时可以上传一些数据项以及上传文件。
/// </summary>
/// <param name="url">要请求的URL地址</param>
/// <param name="keyvalues">要上传的数据项</param>
/// <param name="fileList">要上传的文件列表</param>
/// <param name="encoding">发送数据项,接收的字符编码方式</param>
/// <returns>服务器的返回结果</returns>
public static string SendHttpRequestPost(string url, Dictionary<string, string> keyvalues, Dictionary<string, string> fileList, Encoding encoding)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("URL");
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
string boundary = "------------------" + Guid.NewGuid().ToString("N");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n" + boundary + "\n\r");
request.ContentType = "multipart/form-data; boundary=" + boundary;
Stream stream = request.GetRequestStream();
foreach (var m in keyvalues)
{
// 写入数据块的分隔标记
stream.Write(boundaryBytes, 0, boundaryBytes.Length);
string str = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", m.Key, m.Value);
byte[] date = encoding.GetBytes(str);
stream.Write(date, 0, date.Length);
}
foreach (var m in fileList)
{
// 写入数据块的分隔标记
stream.Write(boundaryBytes, 0, boundaryBytes.Length);
// 写入文件描述,这里设置一个通用的类型描述:application/octet-stream,具体的描述在注册表里有。
string description = string.Format(
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n",
m.Key, Path.GetFileName(m.Value));
// 注意:这里如果不使用UTF-8,对于汉字会有乱码。
byte[] header = Encoding.UTF8.GetBytes(description);
stream.Write(header, 0, header.Length);
// 写入文件内容
byte[] body = File.ReadAllBytes(m.Value);
stream.Write(body, 0, body.Length);
}
// 写入结束标记
boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
stream.Write(boundaryBytes, 0, boundaryBytes.Length);
stream.Close();
using (WebResponse wr = request.GetResponse())
{
StreamReader read = new StreamReader(wr.GetResponseStream(), encoding);
return read.ReadToEnd();
}
}
}