0

1 Hi so I want to display the response of the website in this panel, How would I go about that. I have the necessary System.Net Etc. Heres the code:

 private void panel7_Paint(object sender, PaintEventArgs e)
        {
            WebRequest request = WebRequest.Create("http://www.contoso.com/default.html");
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine(response.StatusDescription);
        }

Would I need to set the webresponse as a string and than pass it as string tx?

[The code of said panel] the code of the panel in cs

[3]: https://i.stack.imgur.com/DBY9w.png //the panel I want the data to display.

Dan c
  • 71
  • 5

1 Answers1

0

I believe you need a label in the panel, then change its label.Text property to the response body.

To read the response body from an HttpWebResponse, please refer to this answer.


I'd also suggest you switch to the modern HttpClient API if you use .NET Core 3 or above:

using var httpClient = new HttpClient();

Example:

using var httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://www.contoso.com/default.html");

using var httpResponse = await httpClient.SendAsync(httpRequest);

label.Text = await httpResponse.Content.ReadAsStringAsync();

Or even simpler, if you don't need to edit the request message at all:

label.Text = await httpClient.GetStringAsync("http://www.contoso.com/default.html");
Matt
  • 72
  • 6