C# Proxy with username and password

浪尽此生 提交于 2020-01-25 16:30:23

问题


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.



来源:https://stackoverflow.com/questions/6029149/c-sharp-proxy-with-username-and-password

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!