0

我正在尝试使用内置的 webclient 类来检索站点的源代码。我找不到解决我遇到的问题的方法:

错误 407:需要代理身份验证

当用java编写时,它工作正常,但我用C#重写它,它每次都会发生。url 不是问题,我已经在它崩溃之前对其进行了调试,并测试了它正在使用的 url。

这是代码:

private String getUrlSource(String url)
{

  using (WebClient client = new WebClient())
  {
      string htmlCode = client.DownloadString(url);
      return htmlCode;
  }
}
4

1 回答 1

0

This is most likely caused by incorrectly configured Proxy property. Also check out related Automatic Proxy Detection article.

Potential fix below, but you'd probably need to find correct proxy configuration in your case.

using (WebClient client = new WebClient {Proxy = WebRequest.DefaultWebProxy })

Note that proxy configuration is per user account, so if code runs from under service's account it may not have correct proxy configured at all - you'd need to manually set correct proxy in code in corresponding configuration file.

于 2013-06-26T19:44:09.323 回答