Friday, April 17, 2009

WebService Authenticate against proxy server.

When proxy server configured for PC or in a network , if it require authentication to make certain request then you have to use following code.

Error: (407) Proxy Authentication Required.

Here i am taking example of SharePoint List service. I added reference of SharePoint List Service.


ListSerivce lst = new ListSerivce();
//Get Default Proxy Setting
IWebProxy proxy = System.Net.WebRequest.DefaultWebProxy;
// Authenticate using network credential. In My case i need authentication using windows account.
proxy.Credential = new System.Net.NetworkCredential(“username”,”password”,”domain”);
lst.Proxy = proxy;
// Make Call to WebService Method.

You can even find System.Net.WebProxy.GetDefaultProxy() return default proxy setting but it is deprecated to better to use WebRequest.DefaultWebProxy.

You can even configure your own webproxy

Lists lst = new Lists();
System.Net.WebProxy proxy = new WebProxy();
proxy.Address = new Uri("
http://proxy:8090");
proxy.Credentials = new NetworkCredential ("username", "password", "domain");
lst.Proxy = proxy;

In both case credential pass as NetworkCredential but you can use CredentialCache to pass other type of credential.

Please give comment on this. Also if you have any better idea please free give your suggestion.

No comments: