Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让WebClient(webservice客户端)自动使用默认代理服务器?_C#_Web Services_Soap_Proxy_Webclient - Fatal编程技术网

C# 如何让WebClient(webservice客户端)自动使用默认代理服务器?

C# 如何让WebClient(webservice客户端)自动使用默认代理服务器?,c#,web-services,soap,proxy,webclient,C#,Web Services,Soap,Proxy,Webclient,我正在从WinForms应用程序调用Web服务。当代理服务器未使用时,一切正常,但是当使用代理时,应用程序崩溃,因为它收到的不是SOAP请求的XML响应,而是一个HTML错误页面,上面写着“需要身份验证” 您似乎可以像这样手动设置代理: WebClient client = new WebClient(); WebProxy wp = new WebProxy("proxy server url here"); client.Proxy = wp; …但在某种程度上,似乎在不执行上述操作的情况

我正在从WinForms应用程序调用Web服务。当代理服务器未使用时,一切正常,但是当使用代理时,应用程序崩溃,因为它收到的不是SOAP请求的XML响应,而是一个HTML错误页面,上面写着“需要身份验证”

您似乎可以像这样手动设置代理:

WebClient client = new WebClient();
WebProxy wp = new WebProxy("proxy server url here");
client.Proxy = wp;
…但在某种程度上,似乎在不执行上述操作的情况下仍能看到代理服务器,因为生成的错误实际上来自代理服务器。它似乎没有从用户的计算机上获取Windows身份验证登录凭据。我怎么能强迫它这么做

在我自己的机器上,如果我使用Fiddler模拟这种情况(并启用“需要代理身份验证”选项),我会弹出一个对话框,询问登录凭据,但这似乎不会发生在我客户的机器上(使用真正的硬件代理-McAfee Web Gateway)

我该怎么办?我是否需要为用户提供一个对话框来手动配置服务器,或者是否有设置告诉WebClient使用Windows默认代理和用户自己的登录凭据

更新

似乎您可以使用下面的代码选择代理服务器,但这并不会导致在所有情况下都显示“身份验证”对话框(在某些PC上有效,但在其他PC上无效):

如果上面的代码是正确的,我不明白为什么一些用户不会被提示输入他们的凭据我是否必须输入自己的代码来收集用户凭据并将其提供给WebRequest对象?

首先尝试使用以下方法:

WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
if (proxy.Address.AbsoluteUri != string.Empty)
{
    Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
    client.Proxy = proxy;
}
如果这不起作用,请尝试:

WebProxy proxy = WebProxy.GetDefaultProxy()
client.Proxy = proxy;
尝试添加

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

到您的app.config文件

using (WebClient webClient = new WebClient())
{

    webClient.UseDefaultCredentials = true;
    webClient.Proxy = WebRequest.GetSystemWebProxy();
}

这应该行得通

你看了吗?不确定这是否相关,因为我的应用程序已经以本地用户的身份运行了,我想?啊,我的坏。。。最近我处理SQL CLRSPROC的次数太多了。。。正如您所指出的,“当您[使用]的只是一个锤子时…”WebClient将“由系统使用配置文件和Internet Explorer局域网设置设置”-。如果未提示客户端输入凭据,则可能是客户端的IE设置不要求输入凭据,这可以通过IE中的站点分类进行配置,或者客户端的代理不需要输入凭据,可能是因为应用程序所访问的站点已在其域中?可能相关:WebPRoxy.GetDefaultProxy已弃用。在web.config中,它也有帮助。应该放在对我不起作用的位置,我仍然得到“需要407代理身份验证”请参见
using (WebClient webClient = new WebClient())
{

    webClient.UseDefaultCredentials = true;
    webClient.Proxy = WebRequest.GetSystemWebProxy();
}