HTTP上传、下载、post、get等工具类
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace SunCreate.CombatPlatform.Server.ComLib
{
/// <summary>
/// Http上传下载文件
/// </summary>
public class HttpUtil
{
#region cookie设置
private static CookieContainer m_Cookie = new CookieContainer();
public static void SetHttpCookie(CookieContainer cookie)
{
m_Cookie = cookie;
}
#endregion
#region HttpDownloadFile 下载文件
public static MemoryStream HttpDownloadFile(string url)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = m_Cookie;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建写入流
MemoryStream stream = new MemoryStream();
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Seek(0, SeekOrigin.Begin);
responseStream.Close();
return stream;
}
#endregion
#region HttpUploadFile 上传文件
/// <summary>
/// Http上传文件
/// </summary>
public static string HttpUploadFile(string url, byte[] bArr, string fileName)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "text/plain;charset=utf-8";
request.CookieContainer = m_Cookie;
Stream postStream = request.GetRequestStream();
postStream.Write(bArr, 0, bArr.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion
#region HttpPost
/// <summary>
/// HttpPost
/// </summary>
public static string HttpPost(string url, string data)
{
byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data);
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = m_Cookie;
request.Method = "POST";
request.ContentType = "text/plain;charset=utf-8";
request.ContentLength = bArr.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(bArr, 0, bArr.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion
#region HttpPost
/// <summary>
/// HttpPost
/// </summary>
public static string HttpPost(string url)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = m_Cookie;
request.Method = "POST";
request.ContentType = "text/plain;charset=utf-8";
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion
#region HttpGet
/// <summary>
/// HttpGet
/// </summary>
public static string HttpGet(string url)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = m_Cookie;
request.Method = "GET";
request.ContentType = "text/plain;charset=utf-8";
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion
}
}
|