^

Get Remote HTML Code as String

Article 95 in C-Sharp

2020-11-22 17:58:28 from bugfish
Get Remote HTML Code as String
C-Sharp / Networking
Last Review on: 2021-05-10 10:16:49 | Hits: 121

Here is an example function to get the full html output code of an URL provided to the function into a string.
You can get the data by using a string variable which gets the output:

string variable = gethtmlcontent("https://thisisapage");

            public string gethtmlcontent(String URL)
            {
                    HttpWebRequest request;
                    HttpWebResponse response;
                        try
                        {   request = (HttpWebRequest)WebRequest.Create(URL);
                            response = (HttpWebResponse)request.GetResponse();
                        }  catch (WebException) { this.searchcode = false;  return null; }
                           catch (Exception) { this.searchcode = false; return null; }

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream receiveStream = response.GetResponseStream();
                        StreamReader readStream;
                        if (String.IsNullOrWhiteSpace(response.CharacterSet)){
                                readStream = new StreamReader(receiveStream);}
                        else {readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));}
                        string data = readStream.ReadToEnd();
                        response.Close();
                        readStream.Close();
                        return data;
                    }
                    
                    this.contextcode = false;
                    response.Close();
                    return null;
            }

I do not guarantee the reliability of the information given here, the code described on this page is executed at your own risk and in the event of damage or other unforeseeable consequences I am in no way responsible or liable.
Comment Section
Currently 0 Upvotes!
captcha image

System - 2021-01-01 19:30:45
We appreciate comments on our site!

This Website is using Session Cookies for Site Functionality and AWStats.
Radio