0

我有一个 ASP MVC 控制器操作。我正在尝试发出网络请求

public ActionResult Index()
{
   WebRequest request = HttpWebRequest.Create("http://www.example.com");
   WebResponse response = request.GetResponse();
   string str =  response.ToString();
}`

我收到“发生 WebException”远程名称无法解析:“www.example.com”

如果我启动 Fiddler,那么 webrequest 就可以了。

我尝试添加:

 <system.net>
 <defaultProxy>
   <proxy usesystemdefault ="True" bypassonlocal="True"  />
 </defaultProxy>

到 Web.config(有和没有 hte bypassonlocal),它仍然不起作用。

有什么建议么?

4

1 回答 1

0

尝试明确指定代理服务器:

<system.net>
    <defaultProxy>
        <proxy proxyaddress="http://proxy.yourcompany.com:80" />
    </defaultProxy>
</system.net>

您还可以以编程方式设置代理:

request.Proxy = new WebProxy("http://proxy.yourcompany.com:80", true);

当您设置usesystemdefault为时,应用程序使用对话框中true定义的代理。Internet Options当您在 IIS 中部署应用程序时,它通常在Network Service权限非常有限的帐户下执行,它甚至没有任何 GUI 会话,因此它无法推断代理服务器。

于 2010-02-02T16:23:16.050 回答