远程服务器返回错误:(407) 需要代理身份验证

发布于 2024-08-27 14:14:34 字数 834 浏览 6 评论 0原文

当我调用 Web 服务时,出现此错误:

“远程服务器返回错误:(407) 需要代理身份验证”。

使代码正常工作

myProxy.Credentials = NetworkCredential("user", "password", "domain");

我了解了总体思路,并且可以通过在代码中添加或使用 DefaultCredentials 来 。我的问题是,如果没有这个,对 Web 服务的调用就可以在生产中正常工作。

似乎有一个涉及 Machine.config 的非代码解决方案,但它是什么?目前我无法访问生产盒的 machine.config 文件来查看它的样子。我尝试按如下方式更新 machine.config,但仍然收到 407 错误。

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <bypasslist>
            <clear />
        </bypasslist>
        <proxy proxyaddress="myproxy:9000"
               usesystemdefault="false"
               bypassonlocal="true"
               autoDetect="False" />
    </defaultProxy>
</system.net>

I'm getting this error when I call a web service:

"The remote server returned an error: (407) Proxy Authentication Required".

I get the general idea and I can get the code to work by adding

myProxy.Credentials = NetworkCredential("user", "password", "domain");

or by using DefaultCredentials in code. My problem is that the call to the web service works in production without this.

It seems like there is a non code solution involving Machine.config, but what is it? At the moment I can't get to the production box's machine.config file to see what that looks like. I tried updating my machine.config as follows, but I still get the 407 error.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <bypasslist>
            <clear />
        </bypasslist>
        <proxy proxyaddress="myproxy:9000"
               usesystemdefault="false"
               bypassonlocal="true"
               autoDetect="False" />
    </defaultProxy>
</system.net>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

素年丶 2024-09-03 14:14:34

只需将其添加到配置中即可

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

Just add this to config

<system.net>
    <defaultProxy useDefaultCredentials="true" >
    </defaultProxy>
</system.net>
聽兲甴掵 2024-09-03 14:14:34

在以下代码中,我们不需要对凭据进行硬编码。

service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

In following code, we don't need to hard code the credentials.

service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
欢烬 2024-09-03 14:14:34

请咨询您的防火墙专家。
他们为 PROD 服务器打开防火墙,因此无需使用代理。

感谢您的提示帮助我解决了我的问题:

必须在两个位置设置凭据才能克服 407 错误:

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

瞧!

Check with your firewall expert.
They open the firewall for PROD servers so there is no need to use the Proxy.

Thanks your tip helped me solve my problem:

Had to to set the Credentials in two locations to get past the 407 error:

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

and voila!

南风几经秋 2024-09-03 14:14:34

可能是prod中的机器或者web.config有配置中的设置;您可能不需要代理标签。

<system.net>
    <defaultProxy useDefaultCredentials="true" >
        <proxy usesystemdefault="False"
               proxyaddress="http://<ProxyLocation>:<port>"
               bypassonlocal="True"
               autoDetect="False" />
    </defaultProxy>
</system.net>

Probably the machine or web.config in prod has the settings in the configuration; you probably won't need the proxy tag.

<system.net>
    <defaultProxy useDefaultCredentials="true" >
        <proxy usesystemdefault="False"
               proxyaddress="http://<ProxyLocation>:<port>"
               bypassonlocal="True"
               autoDetect="False" />
    </defaultProxy>
</system.net>
酷炫老祖宗 2024-09-03 14:14:34
HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;

webRequest.Proxy = WebRequest.DefaultWebProxy;

webRequest.Credentials = new NetworkCredential("user", "password");

webRequest.Proxy.Credentials = new NetworkCredential("user", "password");

它是成功的。

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;

webRequest.Proxy = WebRequest.DefaultWebProxy;

webRequest.Credentials = new NetworkCredential("user", "password");

webRequest.Proxy.Credentials = new NetworkCredential("user", "password");

It is successful.

吾家有女初长成 2024-09-03 14:14:34

写这个答案的想法是因为上面没有任何效果&您不想指定代理位置。

如果您使用的是 httpClient 那么请考虑这一点。

HttpClientHandler handler = new HttpClientHandler();

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
handler.Proxy = proxy;

var client = new HttpClient(handler);
// your next steps...

如果您使用的是 HttpWebRequest

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri + _endpoint);

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = proxy;

请参考:https://medium.com/@siriphonnot/the-remote-server-returned-an-error-407-proxy-authentication-required-86ae489e401b

Thought of writing this answer as nothing worked from above & you don't want to specify proxy location.

If you're using httpClient then consider this.

HttpClientHandler handler = new HttpClientHandler();

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
handler.Proxy = proxy;

var client = new HttpClient(handler);
// your next steps...

And if you're using HttpWebRequest:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri + _endpoint);

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = proxy;

Kind referencce: https://medium.com/@siriphonnot/the-remote-server-returned-an-error-407-proxy-authentication-required-86ae489e401b

遮云壑 2024-09-03 14:14:34

我有类似的代理相关问题。就我而言,添加以下内容就足够了:

webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

I had a similar proxy related problem. In my case it was enough to add:

webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文