Skip to content
Maarten Balliauw
← back
ASP.NET 1 min read

Enabling HTTP proxy for .NET webservice client

edit this page

Have you ever written code that makes external (Soap) webservice calls? Tried that same code on your company network? Most of the time, this does not work very well due to a proxy server sitting in between, requiring authentication etc.

You can start tweaking your Web.config file to set this proxy the right way, or you can override the generated web service class and include the following code snippet:

csharp
using System;
using System.Net;
public class SomethingProxyEnabledService : com.example.service.something {
    protected override System.Net.WebRequest GetWebRequest(Uri uri) {
        WebRequest request = base.GetWebRequest(uri);
        request.Proxy = WebRequest.DefaultWebProxy;
        request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
        return request;
    }
}

The only thing left to do is use this “SomethingProxyEnabledService” class instead of the regular “com.example.service.something”. There you go, automagical proxy authentication!

Maarten Balliauw

Writing about .NET, web, performance and security, since 2006.

Related posts

★ Prefer this blog on Google