5/19/2023 0 Comments Http client in c![]() Public static void DownloadFileAsync(string url, string filename) Public static void DownloadData(string url, string filename)īyte bytes = wc.DownloadData(url) //Download to byte arrayįileStream fs = new FileStream(filename, FileMode.Create) Wc.DownloadFile(url, filename) //Download File Public static void DownloadFile(string url, string filename) StreamReader _read = new StreamReader(objStream, Encoding.UTF8) //Create a new read stream and read it with the specified encoding. Public static string DownloadStreamString(string url) If this line is not added, the Chinese character string obtained will be garbled Wc.Encoding = Encoding.UTF8 //Set the access code. wc.BaseAddress = url // Set root directory Public static string DownloadString(string url) Using webclient may be slower than using Httpwebrequest directly (about a few milliseconds), but it is simpler, reduces a lot of details and less code. However, it should be noted that webclient and WebRequest inherit different classes, and they have no relationship in inheritance. It is equivalent to encapsulating request and response methods. Relatively speaking, webclient is simpler than WebRequest. During use, you will find that it lacks basic header and timeout settings, but these can be realized by inheriting Httpwebrequest. Namespace system Net, webclient is a higher-level abstraction created by Httpwebrequest to simplify the most common tasks. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")) Request.ContentType = "text/html charset=UTF-8" HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr = "" ? "" : "?") + postDataStr) Public static string HttpGet(string Url, string postDataStr) ![]() String retString = myStreamReader.ReadToEnd() ![]() StreamReader myStreamReader = new StreamReader(myResponseStream, encoding) Stream myResponseStream = response.GetResponseStream() HttpWebResponse response = (HttpWebResponse)request.GetResponse() MyRequestStream.Write(postData, 0, postData.Length) Stream myRequestStream = request.GetRequestStream() Request.ContentType = "application/x-www-form-urlencoded" īyte postData = encoding.GetBytes(postDataStr) HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url) Public static string HttpPost(string Url, string postDataStr) HttpWebRquest is more low-level and can have an intuitive understanding of the whole access process, but it is also more complex. HttpWebRequest is usually used with WebResponse, one to send a request and one to get data. For example, when you download a large file from a slow response API server, your application's UI does not stop responding. Another advantage is that the HttpWebRequest class does not block the UI thread. Using HttpWebRequest allows developers to control all aspects of the request / response process, such as timeouts, cookies, headers and protocols. Namespace: system NET, this is NET creators originally developed standard classes for using HTTP requests.
0 Comments
Leave a Reply. |