带有用户名和密码的 C# 代理 [英] C# Proxy with username and password

查看:125
本文介绍了带有用户名和密码的 C# 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个代理实例并将其与 webrequest 对象一起使用.

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

正常工作,但我想在不丢失信息和设计的情况下在 Web 浏览器控件中显示页面.如果我将控件的文档文本设置为刚刚下载的源.它的格式很糟糕.

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.

有没有办法让我将代理对象应用到 Web 浏览器控件本身?

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

推荐答案

edit WebBrowser 控件仅使用 IE 的设置,因此您不必自己设置代理.请参阅 http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f4dc3550-f213-41ff-a17d-95c917bed027/如何在代码中设置IE代理.

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.

这里的问题是您通过 WebRequest 收到的 HTML 包含当前上下文中不存在的 CSS 文件的相对路径.您可以通过在 部分中添加以下标记来修改 HTML:

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/" />

之后,WebBrowser 控件将解析此标记中域的相对 CSS 路径.

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

这篇关于带有用户名和密码的 C# 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆