如何使用IE代理服务器设置及其在NET应用程序凭据凭据、代理服务器、如何使用、应用程序

2023-09-03 17:04:12 作者:本期小编为您设计一组好听又顺口的五个字,好的昵称就应该要朗朗

如何使用保存的代理服务器设置和凭据作为默认HttpWebRequests?代理设置都可以访问和使用,但没有凭据:

  IWebProxy代理= WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy =代理;
 

有没有使用的凭据任何权限?

它的工作原理通过的NetworkCredential实例传递凭据:

  proxy.Credentials =新的NetworkCredential(用户名,密码);
 

不过,我想用的那些保存从操作系统/ IE浏览器。

编辑:

我使用的是第三方的lib创建并呼吁应该通过代理服务器传递的HttpWebRequests。莫非是问题的一部分?

使用与此内容app.config文件也不行

 <结构>
  < system.net>
    < defaultProxy启用=真useDefaultCredentials =真正的>
    < / defaultProxy>
  < /system.net>
< /结构>
 
设置IE代理服务器

解决方案

我也许两个星期前有此相同的问题! HTTP 407错误。我试了两项建议,建立网络凭证,并添加默认的代理线在App.config,但没有奏效。奇怪的一点,因为我没有这个问题在VB应用程序我写的做了同样的事情,去年外出网站并下载源作为一个字符串。当我做它,然后,我就遇到了这个问题,但加入defaultProxy线App.config的工作!怪异。

这是我如何能得到解决此问题。

我不创建一个代理,我给默认的(从app.config中拉)到WebRequest类的代理属性。那么我保证UseDefaultCredentials设置为真。

  HttpWebRequest的要求=(HttpWebRequest.Create(m.Url)为HttpWebRequest的);
                request.Proxy = WebRequest.DefaultWebProxy;
                request.UseDefaultCredentials = TRUE;
 

我已将此添加到的app.config(同上述职位)。

 < system.net>
    < defaultProxy useDefaultCredentials =真/>
  < /system.net>
 

这就是它与众不同(我希望我能解释为什么这解决了我的问题)。之后我创建请求,并指定了正确的凭据代理...我必须创建一个cookie的容器,并把它分配给我的Web请求。说真的,我不知道为什么会这样解决了这个问题,因为我以前是没有这样做。

 的CookieContainer CC =新的CookieContainer();
                request.CookieContainer = CC;
 

我希望这可以帮助你。

下面是完整的code(减去的app.config线):

  HttpWebRequest的要求=(HttpWebRequest.Create(m.Url)为HttpWebRequest的);
        request.Proxy = WebRequest.DefaultWebProxy;
        request.UseDefaultCredentials = TRUE;

        的CookieContainer CC =新的CookieContainer();
        request.CookieContainer = CC;

        HttpWebResponse响应=(request.GetResponse()作为HttpWebResponse);
 

How to use the saved proxy settings and credentials as default for HttpWebRequests? The proxy settings are accessible and used but not the credentials:

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy = proxy;

Are there any permissions on using the credentials?

It works with passing the credentials via NetworkCredential instance:

proxy.Credentials = new NetworkCredential("username", "password");

But I would like to use the saved ones from the operating system/IE.

Edit:

I am using a third party lib creating and calling the HttpWebRequests which should be passed through the proxy. Could that be part of the problem?

Using the App.Config file with this content doesn't work either

<configuration>
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
    </defaultProxy>
  </system.net>
</configuration>

解决方案

I was having this exact same issue maybe two weeks ago!! HTTP 407 errors. I tried both suggestions, set network credentials and add the default proxy line in the app.config but neither worked. Strange too because I didn't have this issue on a vb application I wrote last year that did the exact same thing, go out to a web site and download the source as a string. When I did it then, I ran into this issue but adding the defaultProxy line in App.config worked! Weird.

This is how I was able to get around the issue.

I don't create a proxy, I assign the default one (pulled from app.config) to the webRequest's proxy attribute. I then ensure that "UseDefaultCredentials" is set to "true".

HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest);                
                request.Proxy = WebRequest.DefaultWebProxy;
                request.UseDefaultCredentials = true;

I added this to the app.config (same as the above post).

  <system.net>
    <defaultProxy useDefaultCredentials="true"/>
  </system.net>

Here's the kicker (and I wish I could explain why this solved my issue). Right after I create the request and assign the proxy with the correct credentials... I had to create a cookie container and assign it to my web request. Seriously, I don't know why this solved this issue because I didn't have to do it before.

CookieContainer cc = new CookieContainer();
                request.CookieContainer = cc;

I hope this helps you.

Here's the complete code (minus the app.config lines):

        HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest);                
        request.Proxy = WebRequest.DefaultWebProxy;
        request.UseDefaultCredentials = true;

        CookieContainer cc = new CookieContainer();
        request.CookieContainer = cc;

        HttpWebResponse response = (request.GetResponse() as HttpWebResponse);