Details of the use of HttpWebRequest in C

  • 2020-11-26 18:58:11
  • OfStack

This article illustrates the use of HttpWebRequest in C#. Share to everybody for everybody reference. The details are as follows:

The HttpWebRequest class mainly USES the HTTP protocol and server interaction, usually through GET and POST two ways to obtain and submit data. The following is a description of these two methods:

GET way:
GET way through a network address additional parameters to complete the data submitted, such as the address https: / / www ofstack. com / & # 63; hl = zh - CN, front part https: / / www ofstack. Data submitted url com said, later hl = zh - CN said additional parameters, including hl said a key (key), zh - CN said the key corresponding value (value).
The program code is as follows:

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "https://www.ofstack.com?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
// The content of the page received is processed here
}

Use GET to submit Chinese data.

GET completes data submission by attaching parameters to the network address. For Chinese encoding, gb2312 and utf8 are commonly used.
The program code of gb2312 is as follows:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string address = "https://www.ofstack.com/?" + HttpUtility.UrlEncode(" parameter 1", myEncoding) + "=" + HttpUtility.UrlEncode(" value 1", myEncoding);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
// The content of the page received is processed here
}

In the above code, we visited the site https GET way: / / www ofstack. com, passed parameters of "parameter 1 = 1", unable to inform the other party to submit data coding type, so the encoding to the other side of the web site for the standard.

POST way:

The POST method completes the data submission by filling in the parameters in the page content. The format of the parameters is the same as the GET method, which is similar to hl= ES62en-ES63en & newwindow=1.
The program code is as follows:

string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "https://www.ofstack.com/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
   reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
   // The content of the page received is processed here
}

In the code above, we visited the https: / / www ofstack. com url, with GET POST way to submit the data, and receive the return to the page content. However, if the submitted parameters contain Chinese, then such processing is not enough; it needs to be encoded so that the other site can recognize it.
Use POST to submit Chinese data
The POST method completes the data submission by filling in the parameters in the page content. Since the submitted parameters can explain the encoding method used, it can theoretically achieve greater compatibility.
The program code of gb2312 is as follows:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode(" parameter 1", myEncoding) + "=" + HttpUtility.UrlEncode(" value 1", myEncoding) + "&" + HttpUtility.UrlEncode(" parameter 2", myEncoding) + "=" + HttpUtility.UrlEncode(" value 2", myEncoding);
byte[] postBytes = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "https://www.ofstack.com/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
using (Stream reqStream = req.GetRequestStream())
{
   reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
   // The content of the page received is processed here
}

As can be seen from the above code, when using POST Chinese data, first use UrlEncode method to convert The Chinese characters into the encoded ASCII code, and then submit to the server. When submitting, the encoding method can be explained to enable the server to correctly parse the Chinese data.
Usage of the HttpWebRequest class in C# language
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace HttpWeb
{
    /// <summary>
    ///  Http Action class
    /// </summary>
    public static class httptest
    {
        /// <summary>
        ///  Access to the website HTML
        /// </summary>
        /// <param name="URL"> The url </param>
        /// <returns> </returns>
        public static string GetHtml(string URL)
        {
            WebRequest wrt;
            wrt = WebRequest.Create(URL);
            wrt.Credentials = CredentialCache.DefaultCredentials;
            WebResponse wrp;
            wrp = wrt.GetResponse();
            string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
            try
            {
                wrt.GetResponse().Close();
            }
            catch (WebException ex)
            {
                throw ex;
            }
            return reader;
        }
        /// <summary>
        /// Access to the website cookie
        /// </summary>
        /// <param name="URL"> The url </param>
        /// <param name="cookie">cookie </param>
        /// <returns> </returns>
        public static string GetHtml(string URL, out string cookie)
        {
            WebRequest wrt;
            wrt = WebRequest.Create(URL);
            wrt.Credentials = CredentialCache.DefaultCredentials;
            WebResponse wrp;
            wrp = wrt.GetResponse();             string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
            try
            {
                wrt.GetResponse().Close();
            }
            catch (WebException ex)
            {
                throw ex;
            }
            cookie = wrp.Headers.Get("Set-Cookie");
            return html;
        }
        public static string GetHtml(string URL, string postData, string cookie, out string header, string server)
        {
            return GetHtml(server, URL, postData, cookie, out header);
        }
        public static string GetHtml(string server, string URL, string postData, string cookie, out string header)
        {
            byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);
            return GetHtml(server, URL, byteRequest, cookie, out header);
        }
        public static string GetHtml(string server, string URL, byte[] byteRequest, string cookie, out string header)
        {
            byte[] bytes = GetHtmlByBytes(server, URL, byteRequest, cookie, out header);
            Stream getStream = new MemoryStream(bytes);
            StreamReader streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
            string getString = streamReader.ReadToEnd();
            streamReader.Close();
            getStream.Close();
            return getString;
        }
        /// <summary>
        /// Post Mode to browse
        /// </summary>
        /// <param name="server"> Server address </param>
        /// <param name="URL"> The url </param>
        /// <param name="byteRequest"> flow </param>
        /// <param name="cookie">cookie </param>
        /// <param name="header"> handle </param>
        /// <returns> </returns>
        public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
        {
            long contentLength;
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            Stream getStream;
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            CookieContainer co = new CookieContainer();
            co.SetCookies(new Uri(server), cookie);
            httpWebRequest.CookieContainer = co;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept =
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            httpWebRequest.Referer = server;
            httpWebRequest.UserAgent =
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            httpWebRequest.Method = "Post";
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            header = webResponse.Headers.ToString();
            getStream = webResponse.GetResponseStream();
            contentLength = webResponse.ContentLength;
            byte[] outBytes = new byte[contentLength];
            outBytes = ReadFully(getStream);
            getStream.Close();
            return outBytes;
        }
        public static byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
        /// <summary>
        /// Get model
        /// </summary>
        /// <param name="URL"> The url </param>
        /// <param name="cookie">cookies </param>
        /// <param name="header"> handle </param>
        /// <param name="server"> The server </param>
        /// <param name="val"> The server </param>
        /// <returns> </returns>
        public static string GetHtml(string URL, string cookie, out string header, string server)
        {
            return GetHtml(URL, cookie, out header, server, "");
        }
        /// <summary>
        /// Get Mode to browse
        /// </summary>
        /// <param name="URL">Get The url </param>
        /// <param name="cookie">cookie </param>
        /// <param name="header"> handle </param>
        /// <param name="server"> Server address </param>
        /// <param name="val"> </param>
        /// <returns> </returns>
        public static string GetHtml(string URL, string cookie, out string header, string server, string val)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            Stream getStream;
            StreamReader streamReader;
            string getString = "";
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.Accept = "*/*";
            httpWebRequest.Referer = server;
            CookieContainer co = new CookieContainer();
            co.SetCookies(new Uri(server), cookie);
            httpWebRequest.CookieContainer = co;
            httpWebRequest.UserAgent =
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            httpWebRequest.Method = "GET";
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            header = webResponse.Headers.ToString();
            getStream = webResponse.GetResponseStream();
            streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
            getString = streamReader.ReadToEnd();
            streamReader.Close();
            getStream.Close();
            return getString;
        }
   }
}

Hopefully this article has been helpful for your C# programming.


Related articles: