usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Text;
namespaceSunCreate.CombatPlatform.Server.ComLib
{
///<summary>
///Http上传下载文件
///</summary>
publicclassHttpUtil
{
#regioncookie设置
privatestaticCookieContainerm_Cookie=newCookieContainer();
publicstaticvoidSetHttpCookie(CookieContainercookie)
{
m_Cookie=cookie;
}
#endregion
#regionHttpDownloadFile下载文件
publicstaticMemoryStreamHttpDownloadFile(stringurl)
{
//设置参数
HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
request.Method=”GET”;
request.CookieContainer=m_Cookie;
//发送请求并获取相应回应数据
HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
StreamresponseStream=response.GetResponseStream();
//创建写入流
MemoryStreamstream=newMemoryStream();
byte[]bArr=newbyte[1024];
intsize=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();
returnstream;
}
#endregion
#regionHttpUploadFile上传文件
///<summary>
///Http上传文件
///</summary>
publicstaticstringHttpUploadFile(stringurl,byte[]bArr,stringfileName)
{
//设置参数
HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
CookieContainercookieContainer=newCookieContainer();
request.CookieContainer=cookieContainer;
request.AllowAutoRedirect=true;
request.Method=”POST”;
stringboundary=DateTime.Now.Ticks.ToString(“X”);//随机分隔线
request.ContentType=”text/plain;charset=utf-8″;
request.CookieContainer=m_Cookie;
StreampostStream=request.GetRequestStream();
postStream.Write(bArr,0,bArr.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Streaminstream=response.GetResponseStream();
StreamReadersr=newStreamReader(instream,Encoding.UTF8);
//返回结果网页(html)代码
stringcontent=sr.ReadToEnd();
returncontent;
}
#endregion
#regionHttpPost
///<summary>
///HttpPost
///</summary>
publicstaticstringHttpPost(stringurl,stringdata)
{
byte[]bArr=ASCIIEncoding.UTF8.GetBytes(data);
//设置参数
HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
request.CookieContainer=m_Cookie;
request.Method=”POST”;
request.ContentType=”text/plain;charset=utf-8″;
request.ContentLength=bArr.Length;
StreampostStream=request.GetRequestStream();
postStream.Write(bArr,0,bArr.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Streaminstream=response.GetResponseStream();
StreamReadersr=newStreamReader(instream,Encoding.UTF8);
//返回结果网页(html)代码
stringcontent=sr.ReadToEnd();
returncontent;
}
#endregion
#regionHttpPost
///<summary>
///HttpPost
///</summary>
publicstaticstringHttpPost(stringurl)
{
//设置参数
HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
request.CookieContainer=m_Cookie;
request.Method=”POST”;
request.ContentType=”text/plain;charset=utf-8″;
//发送请求并获取相应回应数据
HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Streaminstream=response.GetResponseStream();
StreamReadersr=newStreamReader(instream,Encoding.UTF8);
//返回结果网页(html)代码
stringcontent=sr.ReadToEnd();
returncontent;
}
#endregion
#regionHttpGet
///<summary>
///HttpGet
///</summary>
publicstaticstringHttpGet(stringurl)
{
//设置参数
HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
request.CookieContainer=m_Cookie;
request.Method=”GET”;
request.ContentType=”text/plain;charset=utf-8″;
//发送请求并获取相应回应数据
HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Streaminstream=response.GetResponseStream();
StreamReadersr=newStreamReader(instream,Encoding.UTF8);
//返回结果网页(html)代码
stringcontent=sr.ReadToEnd();
returncontent;
}
#endregion
}
}
来源:志在指尖