using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text; namespace Allyn.Common
{
public class HttpHelper
{
/// <summary>
/// 获取指定路径数据
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string GetForm(string requestUri, CookieContainer cookie)
{
HttpWebRequest request = WebRequest.CreateHttp(requestUri);
request.Method = "get";
request.CookieContainer = cookie;
request.ContentLength = ; WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
} /// <summary>
/// 默认表单提交
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="postData">提交数据</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string PostForm(string requestUri, NameValueCollection postData, CookieContainer cookie)
{
HttpWebRequest request = WebRequest.CreateHttp(requestUri);
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookie; StringBuilder stringBuilder = new StringBuilder();
foreach (string key in postData.Keys)
{
stringBuilder.AppendFormat("&{0}={1}", key, postData.Get(key));
}
byte[] buffer = Encoding.UTF8.GetBytes(stringBuilder.ToString().Trim('&'));
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, , buffer.Length);
requestStream.Close(); WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
} /// <summary>
/// 多部件表单提交
/// </summary>
/// <param name="requestUri">提交路径</param>
/// <param name="postData">提交数据.注:如果是文件路径,代表是文件.</param>
/// <param name="cookie">Cookie容器对象</param>
/// <returns>字符串结果</returns>
public static string PostFormMultipart(string requestUri, NameValueCollection postData, CookieContainer cookie)
{
string boundary = string.Format("-----{0}", DateTime.Now.Ticks.ToString("x"));
HttpWebRequest webrequest = WebRequest.CreateHttp(requestUri);
webrequest.CookieContainer = cookie;
webrequest.Timeout = ;
webrequest.Method = "post";
webrequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); Stream requestStream = webrequest.GetRequestStream();
foreach (string key in postData.Keys)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("--{0}", boundary);
strBuilder.AppendFormat("\r\nContent-Disposition: form-data; name=\"{0}\"", key);
if (File.Exists(postData.Get(key)))
{
strBuilder.AppendFormat(";filename=\"{0}\"\r\nContent-Type: multipart/form-data\r\n\r\n", Path.GetFileName(postData.Get(key)));
byte[] buffer = Encoding.UTF8.GetBytes(strBuilder.ToString());
requestStream.Write(buffer, , buffer.Length);
//获取图片流
FileStream fileStream = new FileStream(postData.Get(key), FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] fileBuffer = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
fileStream.Close();
requestStream.Write(fileBuffer, , fileBuffer.Length);
}
else
{
strBuilder.AppendFormat("\r\n\r\n{0}\r\n", postData.Get(key));
byte[] buff = Encoding.UTF8.GetBytes(strBuilder.ToString());
requestStream.Write(buff, , buff.Length);
}
} byte[] boundaryBuffer = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}\r\n", boundary));
requestStream.Write(boundaryBuffer, , boundaryBuffer.Length);
requestStream.Close(); WebResponse response = webrequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}
}
}
05-11 13:27