Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    My projects

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Enabling HTTP proxy for .NET webservice client

    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:

    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!


    Categories: ASP.NET | C# | General

    Comments

    Pablo Szittyay Argentina | Reply

    Wednesday, May 14, 2008 11:02 PM

    Pablo Szittyay

    I have an application that consumes from a RssFeed. All works fine in development, but when i try in the server (Windows Server 2003), with a proxy configuration i get the folliwing error.

    Service name could not be resolved

    The server has a proxy configuration. Do you know how to cnfigure the WebRequest object, so that it uses the proxy.

    maartenba | Reply

    Thursday, May 15, 2008 7:36 AM

    maartenba

    You can set request.Proxy to a custom Proxy instance which mimics the configuration on your server.

    Pablo Szittyay Argentina | Reply

    Thursday, May 15, 2008 4:36 PM

    Pablo Szittyay

    Thanks for respondig so soon. Woled it be to much trouble to give me an example of how to configure the  proxy of the web request. I have tried, using the following code:

                    WebRequest request = WebRequest.Create(forecastUrl);
                    request.Proxy = new WebProxy ("172.16.15.235",8080);
                    request.Credentials = new NetworkCredential ("degas","xxxx","172.16.15.235:8080");
                    WebResponse response = request.GetResponse();

    On the proxy (wich is the way out to consume the Rssfeed) and i have set the credentials of the proxy.
    It doesn`t work, i sure that i am doing something wrong. Do you know what? if you have an example it would be great.
    Thanks a lot

    Pablo Szittyay Argentina | Reply

    Thursday, May 15, 2008 4:56 PM

    Pablo Szittyay

    Sorry, the code i posted before worked like a charm, it was a problem with the user "degas". Thanks a lot

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading