代碼: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace SunCreate.Comba ...
代碼:
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 } }View Code
標簽:HTTP Post