Home > Software design >  Winforms WebClient not getting the webpage properly
Winforms WebClient not getting the webpage properly

Time:12-22

The page originally shows prices and items, but at the time of obtaining the result only the header is seen.

private void Form1_Load(object sender, EventArgs e)
        {
            string q = "https://www.g2g.com/categories/counter-strike-global-offensive-item";
            using (WebClient client = new WebClient())
            {
                client.Headers.Add("user-agent", "foo");
                client.UseDefaultCredentials = true;
                client.Headers[HttpRequestHeader.UserAgent] = "Mozilla / 5.0(Windows NT 10.0; Win64; x64; rv: 44.0) Gecko / 20100101 Firefox / 44.0";
                client.Encoding = Encoding.UTF8;
                string htmlcode = client.DownloadString(q);
                asss.Text = htmlcode;
            }
        }
    } 

How it is

photo

What i get

photo

CodePudding user response:

it seems that you forgot to use a control that renders HTML and executes JavaScript (like WebBrowser or the newer WebView2) instead of a normal TextBox or LinkLabel (as indicated by the plaintext content).

You can set the page contents that you retrieved with WebClient via WebBrowser.DocumentText.

  • Related