HTTP request tool and request Tool
HTTP request tool class, applicable to server requests, can be self-tested
Code;
1 /// <summary> 2 // HTTP request tool class 3 /// </summary> 4 public class HttpRequestUtil 5 {6 # region request Url 7 8 # region request Url, do not send data 9 // <summary> 10 // request Url, do not send data 11 /// </summary> 12 public static string RequestUrl (string url) 13 {14 return RequestUrl (url, "POST"); 15} 16 # endregion 17 18 # region request Url, do not send data 19 // <summary> 20 // request Url, do not send data 21 /// </summary> 22 public static string RequestUrl (string url, string method) 23 {24 // set parameter 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 // send the request and obtain the response data 34 HttpWebResponse response = request. getResponse () as HttpWebResponse; 35 // until request. the GetResponse () program starts to send the Post request 36 Stream responseStream = response to the target webpage. getResponseStream (); 37 StreamReader sr = new StreamReader (responseStream, Encoding. UTF8); 38 // return result webpage (html) code 39 string content = sr. readToEnd (); 40 return content; 41} 42 # endregion 43 44 # region request Url, send data 45 /// <summary> 46 // request Url, send data 47 // </summary> 48 public static string PostUrl (string url, string postData) 49 {50 byte [] data = Encoding. UTF8.GetBytes (postData); 51 52 // set the parameter 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 // send the request and obtain the response data 65 HttpWebResponse response = request. getResponse () as HttpWebResponse; 66 // until request. the GetResponse () program starts to send the Post request 67 Stream instream = response to the target webpage. getResponseStream (); 68 StreamReader sr = new StreamReader (instream, Encoding. UTF8); 69 // return result webpage (html) code 70 string content = sr. readToEnd (); 71 return content; 72} 73 # endregion 74 75 # endregion 76 77 # region Http download file 78 // <summary> 79 // Http download file 80 // </summary> 81 public static string HttpDownloadFile (string url, string path) 82 {83 // set the parameter 84 HttpWebRequest request = WebRequest. create (url) as HttpWebRequest; 85 86 // send the request and obtain the response data 87 HttpWebResponse response = request. getResponse () as HttpWebResponse; 88 // until request. the GetResponse () program starts to send the Post request 89 Stream responseStream = response to the target webpage. getResponseStream (); 90 91 // create local file write Stream 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 # endregion106 107 # region Http Upload File 108 // <summary> 109 // Http Upload File 110 /// </summary> 111 public static string HttpUploadFile (string url, string path) 112 {113 // set the parameter 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"); // random separator 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 // request header information 128 StringBuilder sbHeader = new StringBuilder (string. format ("Content-Disposition: form-data; name = \" file \ "; filename = \" {0} \ "\ r \ nContent-Type: application/octet-stream \ 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 // send the request and obtain the response data 144 HttpWebResponse response = request. getResponse () as HttpWebResponse; 145 // until request. the GetResponse () program starts to send the Post request 146 Stream instream = response to the target webpage. getResponseStream (); 147 StreamReader sr = new StreamReader (instream, Encoding. UTF8); 148 // The returned result webpage (html) code 149 string content = sr. readToEnd (); 150 return content; 151} 152 # endregion153 154}View Code
Any questions can be communicated at any time