我有一个ASP MVC的控制器操作。 我试图做一个Web请求

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),它仍然不工作。

任何建议?

有帮助吗?

解决方案

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

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

您也可以设置代理编程:

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top