I'm currently developing an IE plugin using SpicIE.
我目前正在使用SpicIE开发一个IE插件。
This plugin does some web scraping similar to the example posted on MSDN:
这个插件做了一些网页抓取,类似于MSDN上发布的示例:
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
Stream dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
reader.Close ();
dataStream.Close ();
response.Close ();
However, when i run this code i receive the following error message:
但是,当我运行此代码时,我收到以下错误消息:
The remote server returned an error: (407) Proxy Authentication Required.
远程服务器返回错误:(407)需要代理身份验证。
I'm currently working behind a proxy server and used the NetworkCredential class to manually provide my network credentials
我目前正在代理服务器后面工作,并使用NetworkCredential类手动提供我的网络凭据
request.Credentials = new System.Net.NetworkCredential("name", "password", "domain");
but i still receive the same error.
但我仍然收到同样的错误。
Even if my problem is solved, i know that some users of the plugin will be behind a proxy server.
即使我的问题解决了,我知道该插件的一些用户将在代理服务器后面。
I want to know how i can get IE credentials and use it in my code to assign it to request.Credentials.
我想知道如何获取IE凭据并在我的代码中使用它来将其分配给request.Credentials。
Maybe something like this:
也许是这样的:
request.Credentials = IE.DefaultCredentials;
1 个解决方案
#1
You're setting the credentials for the site, but you need credentials for the proxy.
您正在设置站点的凭据,但您需要代理的凭据。
Set request.Proxy.Credentials
.
(Also, use using
statements for the response/stream/reader rather than manually closing them, otherwise they'll leak when an exception is thrown.)
(另外,使用using语句作为response / stream / reader而不是手动关闭它们,否则在抛出异常时它们会泄漏。)
EDIT: For instance, to use the default credentials for the proxy as well:
编辑:例如,要使用代理的默认凭据:
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
#1
You're setting the credentials for the site, but you need credentials for the proxy.
您正在设置站点的凭据,但您需要代理的凭据。
Set request.Proxy.Credentials
.
(Also, use using
statements for the response/stream/reader rather than manually closing them, otherwise they'll leak when an exception is thrown.)
(另外,使用using语句作为response / stream / reader而不是手动关闭它们,否则在抛出异常时它们会泄漏。)
EDIT: For instance, to use the default credentials for the proxy as well:
编辑:例如,要使用代理的默认凭据:
request.Proxy.Credentials = CredentialCache.DefaultCredentials;