Http WebRequest / Response无法使用代理设置 [英] Http WebRequest/Response not working with proxy settings

查看:107
本文介绍了Http WebRequest / Response无法使用代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发一个需要检测互联网并从服务器下载一些文件的应用程序。我正在使用http webrequest /响应逻辑来进行互联网检测逻辑。它工作正常,但如果在具有代理服务器的网络上尝试,则会失败。



我在公司网络上,请求必须通过代理。为了克服这个问题,我在谷歌搜索后使用了以下逻辑。



HttpWebRequest webRequest =

(HttpWebRequest)(WebRequest.Create(http ://www.google.com));

IWebProxy proxy = WebRequest.GetSystemWebProxy();

proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

webRequest.Proxy = proxy;



HttpWebResponse webResponse =(HttpWebResponse)(webRequest.GetResponse());



有了它,它开始工作,但依赖于IE->连接 - >局域网设置中的以下设置



1)自动检测设置

2)使用自动配置脚本



需要选择这些选项才能使上述逻辑起作用。但是如果我只选择了指向有效脚本的第二个选项(使用自动配置脚本),我就可以浏览互联网。所以,如果只选择此选项,我的期望是逻辑也应该有效。但它给出了以下例外



远程名称无法解析:''www.google.com''



但是,如果在IE LAN设置中选择了选项1和2,则上述逻辑工作正常。



请帮助我理解这种行为的原因以及如何解决。



提前致谢。

Hi All,

I am working on a application which needs to detect internet and download some files from server. I am using http webrequest/response logic for internet detect logic. It is working fine but fails if tried on network which has proxy server in place.

I am on a corporate network and the request has to go through the proxy. To overcome this I used the below logic after searching in google.

HttpWebRequest webRequest =
(HttpWebRequest) (WebRequest.Create("http://www.google.com"));
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
webRequest.Proxy = proxy;

HttpWebResponse webResponse =(HttpWebResponse) (webRequest.GetResponse());

With this it started working but has dependency on the following settings in IE->Connections->LAN Settings

1) Automatically detect settings
2) Use automatic configuration script

Both these options needs to be selected for the above logic to work. But I am able to browse internet if I have just the 2nd option (Use automatic configuration script) selected which points to a valid script. So, my expectaion is the logic should also work, if just this option is selected. But it gives the below exception

The remote name could not be resolved: ''www.google.com''

However, if both option 1 and 2 are selected in IE LAN settings, then the above logic works fine.

Please help me understand the reason for this behavior and how to solve.

Thanks in advance.

推荐答案





这里有一个示例如何使用凭证手动配置代理。


这篇关于Http WebRequest / Response无法使用代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆