如何使用代理设置异步访问webservice?
如何使用代理设置访问webservice?
通过添加web引用,同步调用webservice的时候,s 有Proxy属性设置代理,程序如下:
Service1 s = new Service1();
if (Properties.Settings.Default.UseProxy)
{
System.Net.WebProxy oWebProxy = new System.Net.WebProxy(Properties.Settings.Default.ProxyAdd, Properties.Settings.Default.ProxyPort);
oWebProxy.Credentials = new NetworkCredential(Properties.Settings.Default.ProxyUsername, Properties.Settings.Default.ProxyPassword, Properties.Settings.Default.ProxyDomain);
s.Proxy = oWebProxy;
ServicePointManager.Expect100Continue = false;
}
但是我通过 添加服务引用 来异步调用 webservice的时候,s 没有Proxy属性设置代理,程序如下:
webServiceTest.ServiceReference1.Service1SoapClient s = new webServiceTest.ServiceReference1.Service1SoapClient();
if (Properties.Settings.Default.UseProxy)
{
System.Net.WebProxy oWebProxy = new System.Net.WebProxy(Properties.Settings.Default.ProxyAdd, Properties.Settings.Default.ProxyPort);
oWebProxy.Credentials = new NetworkCredential(Properties.Settings.Default.ProxyUsername, Properties.Settings.Default.ProxyPassword, Properties.Settings.Default.ProxyDomain);
s.Proxy = oWebProxy;//s没有属性Proxy
ServicePointManager.Expect100Continue = false;
}
那么,采用异步调用的时候怎么使用代理设置异步访问webservice?
------解决方案--------------------
直接通过配置也行啊。另外通过代码设置 HttpWebRequest.DefaultWebProxy 就可以了。
不用每次调用时设置。
C# code
HttpWebRequest.DefaultWebProxy = new WebProxy("http://10.1.40.253:8080", true);
HttpWebRequest.DefaultWebProxy.Credentials =
new NetworkCredential { UserName = "xxx", Password = "xxx" };