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-10-26 18:57:05 | Hits: 246

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;
                        {   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();
                        return data;
                    this.contextcode = false;
                    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.
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.