C# Proxy with username and password

2019-08-24 12:07发布

问题:

I set up a proxy instance and used it with a webrequest object.

        WebProxy a = new WebProxy("ip:port", true);
        proxy.Credentials = new NetworkCredential("username", "password");

        WebRequest b = WebRequest.Create("webpage url");
        b.Proxy = proxy;

        WebResponse c = req.GetResponse();

        StreamReader d = new StreamReader(c.GetResponseStream());

        d.ReadToEnd();//web page source

Works as it should, but I want to display the page in a web browser control without loss of information and design. If I set my control's document text to the source that was just downloaded. It has very bad formatting.

edit: Is there a way for me to apply the proxy object to the web browser control itself?

回答1:

edit The WebBrowser control just uses IE's settings, so you don't have to set the proxy yourself. See http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f4dc3550-f213-41ff-a17d-95c917bed027/ how to set the IE proxy in code.


Well the problem here is that the HTML that you've received via the WebRequest contains relative paths to the CSS files that are not present in the current context. You can modify the HTML, by adding the following tag in the <head> section:

<base href="http://domainname.com/" />

After that the WebBrowser control resolves the relative CSS paths to the domain in this tag.