HTTP请求工具类
HTTP请求工具类,适用于微信服务器请求,可以自测
代码;
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 }
有什么问题可以随时沟通