我怎样才能获得IE凭证在我的代码中使用? [英] How can i get IE credentials to use in my code?
问题描述
我目前正在开发使用 SpicIE 一个IE浏览器插件。
本插件做一些网页刮类似于发布MSDN上的例子:
<$ C C>的WebRequest请求= WebRequest.Create(http://www.contoso.com/default.html)$;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
流数据流= response.GetResponseStream();
StreamReader的读者=新的StreamReader(数据流);
串responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
然而,当我运行此代码我收到以下错误信息:
远程服务器返回错误:(407)代理身份验证
块引用>
我目前正在使用代理服务器,并使用了的NetworkCredential 类手动提供我的网络凭据
request.Credentials =新System.Net.NetworkCredential(名,密码,域);
但我仍然收到同样的错误。
即使我的问题解决了,我知道该插件的一些用户将使用代理服务器。
我想知道我怎么能得到IE浏览器证书,并用它在我的代码将其分配到request.Credentials 。
也许是这样的:
request.Credentials = IE浏览器。的DefaultCredentials;
解决方案您所设置的凭据的网站的,但你需要的的代理的
设置的
request.Proxy.Credentials
。
(此外,使用
使用
语句响应/流/读取器而不是手动关闭它们,否则它们就会泄漏时。将抛出一个异常)
编辑:例如,要使用代理服务器的默认凭据,以及:
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
I'm currently developing an IE plugin using SpicIE.
This plugin does some web scraping similar to the example posted on 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.
I'm currently working behind a proxy server and used the NetworkCredential class to manually provide my network credentials
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.
Maybe something like this:
request.Credentials = IE.DefaultCredentials;
解决方案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.)EDIT: For instance, to use the default credentials for the proxy as well:
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
这篇关于我怎样才能获得IE凭证在我的代码中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!