HTTP請求工具類,適用於微信服務器請求,能夠自測html
代碼;服務器
1 /// <summary> 2 /// HTTP請求工具類 3 /// </summary> 4 public class HttpRequestUtil 5 { 6 #region 請求Url 7 8 #region 請求Url,不發送數據 9 /// <summary> 10 /// 請求Url,不發送數據 11 /// </summary> 12 public static string RequestUrl(string url) 13 { 14 return RequestUrl(url, "POST"); 15 } 16 #endregion 17 18 #region 請求Url,不發送數據 19 /// <summary> 20 /// 請求Url,不發送數據 21 /// </summary> 22 public static string RequestUrl(string url, string method) 23 { 24 // 設置參數 25 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 26 CookieContainer cookieContainer = new CookieContainer(); 27 request.CookieContainer = cookieContainer; 28 request.AllowAutoRedirect = true; 29 request.Method = method; 30 request.ContentType = "text/html"; 31 request.Headers.Add("charset", "utf-8"); 32 33 //發送請求並獲取相應迴應數據 34 HttpWebResponse response = request.GetResponse() as HttpWebResponse; 35 //直到request.GetResponse()程序纔開始向目標網頁發送Post請求 36 Stream responseStream = response.GetResponseStream(); 37 StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); 38 //返回結果網頁(html)代碼 39 string content = sr.ReadToEnd(); 40 return content; 41 } 42 #endregion 43 44 #region 請求Url,發送數據 45 /// <summary> 46 /// 請求Url,發送數據 47 /// </summary> 48 public static string PostUrl(string url, string postData) 49 { 50 byte[] data = Encoding.UTF8.GetBytes(postData); 51 52 // 設置參數 53 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 54 CookieContainer cookieContainer = new CookieContainer(); 55 request.CookieContainer = cookieContainer; 56 request.AllowAutoRedirect = true; 57 request.Method = "POST"; 58 request.ContentType = "application/x-www-form-urlencoded"; 59 request.ContentLength = data.Length; 60 Stream outstream = request.GetRequestStream(); 61 outstream.Write(data, 0, data.Length); 62 outstream.Close(); 63 64 //發送請求並獲取相應迴應數據 65 HttpWebResponse response = request.GetResponse() as HttpWebResponse; 66 //直到request.GetResponse()程序纔開始向目標網頁發送Post請求 67 Stream instream = response.GetResponseStream(); 68 StreamReader sr = new StreamReader(instream, Encoding.UTF8); 69 //返回結果網頁(html)代碼 70 string content = sr.ReadToEnd(); 71 return content; 72 } 73 #endregion 74 75 #endregion 76 77 #region Http下載文件 78 /// <summary> 79 /// Http下載文件 80 /// </summary> 81 public static string HttpDownloadFile(string url, string path) 82 { 83 // 設置參數 84 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 85 86 //發送請求並獲取相應迴應數據 87 HttpWebResponse response = request.GetResponse() as HttpWebResponse; 88 //直到request.GetResponse()程序纔開始向目標網頁發送Post請求 89 Stream responseStream = response.GetResponseStream(); 90 91 //建立本地文件寫入流 92 Stream stream = new FileStream(path, FileMode.Create); 93 94 byte[] bArr = new byte[1024]; 95 int size = responseStream.Read(bArr, 0, (int)bArr.Length); 96 while (size > 0) 97 { 98 stream.Write(bArr, 0, size); 99 size = responseStream.Read(bArr, 0, (int)bArr.Length); 100 } 101 stream.Close(); 102 responseStream.Close(); 103 return path; 104 } 105 #endregion 106 107 #region Http上傳文件 108 /// <summary> 109 /// Http上傳文件 110 /// </summary> 111 public static string HttpUploadFile(string url, string path) 112 { 113 // 設置參數 114 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 115 CookieContainer cookieContainer = new CookieContainer(); 116 request.CookieContainer = cookieContainer; 117 request.AllowAutoRedirect = true; 118 request.Method = "POST"; 119 string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線 120 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; 121 byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); 122 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); 123 124 int pos = path.LastIndexOf("\\"); 125 string fileName = path.Substring(pos + 1); 126 127 //請求頭部信息 128 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); 129 byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); 130 131 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); 132 byte[] bArr = new byte[fs.Length]; 133 fs.Read(bArr, 0, bArr.Length); 134 fs.Close(); 135 136 Stream postStream = request.GetRequestStream(); 137 postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); 138 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); 139 postStream.Write(bArr, 0, bArr.Length); 140 postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); 141 postStream.Close(); 142 143 //發送請求並獲取相應迴應數據 144 HttpWebResponse response = request.GetResponse() as HttpWebResponse; 145 //直到request.GetResponse()程序纔開始向目標網頁發送Post請求 146 Stream instream = response.GetResponseStream(); 147 StreamReader sr = new StreamReader(instream, Encoding.UTF8); 148 //返回結果網頁(html)代碼 149 string content = sr.ReadToEnd(); 150 return content; 151 } 152 #endregion 153 154 }
有什麼問題能夠隨時溝通微信