Geeks With Blogs

News My Blog has been MOVED to
Michael Freidgeim's OLD Blog My Blog has been MOVED to

Some time ago I wrote post "Call Web Services through SQuid proxy server with authentication requested"  and hoped, that I will not have more problems with Squid web proxy server.
However teh solution wasn't sufficient for other site. I've tried a lot of things, started thread "Unable to access external web sites through Squid proxy server with required authentication." and posted suggestion to Microsoft "Add ability to specify credentials to defaultProxy configuration element".

But the best solution, that I found for the moment is  to create common function to set web proxy credentials and call it  for each Web Services call,HttpWebRequest call and replace  dataset.ReadXml(url)  with HttpWebRequest.GetRequest.
The function created expects up to 4 properties in appSettings section of Web.config
        <!--Web Proxy Credentials to use if useDefaultCredentials="true" doesn't help

    TODO move to separate ConfigurationSection, different from AppSettings

    <add key="ProxyUserName" value=""/>

    <add key="ProxyUserPassword" value=""/>

    <add key="ProxyAuthenticationType" value="Basic"/>

    <add key="ProxyUserDomain" value=""/>

and to be called like the following for web services


        AWSECommerceService aws = new AWSECommerceService();



or for dataset.ReadXml


        IWebProxy proxyObject = CreateDefaultWebProxyWithCredentialsIfRequired(url);//WSClientHelper.

        if (proxyObject == null)




            Stream strm = GetResponseStream(url, proxyObject);//HttpWebRequestHelper.




The code of the SetDefaultWebProxyCredentialsIfRequired and other helper methods is the following:


    public static bool SetDefaultWebProxyCredentialsIfRequired(SoapHttpClientProtocol ws)


        IWebProxy proxyObject = CreateDefaultWebProxyWithCredentialsIfRequired(ws.Url);

        //bool bRet =SetDefaultWebProxyCredentialsIfRequired(ws, ProxyUserName, ProxyUserPassword, sAuthType, ProxyUserDomain);

        bool bRet = SetDefaultWebProxyCredentials(ws, proxyObject);

        return bRet;


    /// <summary>


    /// </summary>

    /// <param name="ws"></param>

    /// <param name="proxyObject">if null, no proxy is set</param>

    /// <returns></returns>

    public static bool SetDefaultWebProxyCredentials(SoapHttpClientProtocol ws, IWebProxy proxyObject)


        bool bRet = false;

        if (null == proxyObject) return false;

            ws.Proxy = proxyObject;

            ws.PreAuthenticate = true;//from

            ws.AllowAutoRedirect = true;

            bRet = true;

            //ws.Credentials = cache;

        return bRet;


    /// <summary>

    /// If configuration parameters for ProxyUserName and ProxyUserPassword are specified, returns IWebProxy object, otherwise returns null

    /// </summary>

    /// <param name="sUrl"></param>

    /// <returns></returns>

    public static IWebProxy CreateDefaultWebProxyWithCredentialsIfRequired(String sUrl)


        //TODO move to separate ConfigurationSection, different from AppSettings

        string ProxyUserName = ConfigurationManager.AppSettings["ProxyUserName"];

        string ProxyUserPassword = ConfigurationManager.AppSettings["ProxyUserPassword"];

        string sAuthType = ConfigurationManager.AppSettings["ProxyAuthenticationType"];

        string ProxyUserDomain = ConfigurationManager.AppSettings["ProxyUserDomain"];

        IWebProxy proxyObject = CreateWebProxyWithCredentials(sUrl, ProxyUserName, ProxyUserPassword, sAuthType, ProxyUserDomain);

        return proxyObject;


    /// <summary>


    /// </summary>

    /// <param name="sUrl"></param>

    /// <param name="ProxyUserName">if parameter is null, the function returns null</param>

    /// <param name="ProxyUserPassword">if parameter is null, the function returns null</param>

    /// <param name="sAuthType">defaults to "basic"</param>

    /// <param name="ProxyUserDomain">ignored for "basic" authentication type</param>

    /// <returns></returns>

    public static IWebProxy CreateWebProxyWithCredentials(String sUrl, string ProxyUserName, string ProxyUserPassword, string sAuthType, string ProxyUserDomain)


        if (String.IsNullOrEmpty(ProxyUserName) || String.IsNullOrEmpty(ProxyUserPassword))


            return null;


        // get default proxy and assign it to the WebService. Alternatively, you can replace this with manual WebProxy creation.

        IWebProxy iDefaultWebProxy = WebRequest.DefaultWebProxy;

        Uri uriProxy = iDefaultWebProxy.GetProxy(new Uri(sUrl));

        string sProxyUrl = uriProxy.AbsoluteUri;

        if (sProxyUrl == sUrl)

        {//no proxy specified

            return null;


        IWebProxy proxyObject = new WebProxy(sProxyUrl, true);

        // assign the credentials to the Proxy

        //todo do we need to add credentials to  WebService too??

        if ((!String.IsNullOrEmpty(sAuthType)) && (sAuthType.ToLower() != "basic"))



            // create credentials cache - it will hold both, the WebProxy credentials (??and the WebService credentials too??)

            System.Net.CredentialCache cache = new System.Net.CredentialCache();

            // add default credentials for Proxy (notice the authType = 'Kerberos' !) Other types are 'Basic', 'Digest', 'Negotiate', 'NTLM'

            cache.Add(new Uri(sProxyUrl), sAuthType, new System.Net.NetworkCredential(ProxyUserName, ProxyUserPassword, ProxyUserDomain));

            proxyObject.Credentials = cache;


        else//special case for Basic (from )


            proxyObject.Credentials = new System.Net.NetworkCredential(ProxyUserName, ProxyUserPassword);


        return proxyObject;


    //Default methos is Get, do not store cookies

    public static Stream GetResponseStream(string sUrl, IWebProxy proxy)

    {// string Method, CookieContainer cntnrCookies, string sFileToSaveHtml

        //            HttpWebRequest webRequest = HttpWebRequestHelper.PrepareWebRequest(sUrl, Method, cntnrCookies);

        HttpWebRequest webRequest = WebRequest.Create(sUrl) as HttpWebRequest;

        //  webRequest.ContentType = "application/x-www-form-urlencoded";//is good?

        webRequest.Proxy = proxy;

        //you can  change some properties if required - from         

        //MNF 26/5/2005 Some web servers expect UserAgent to be specified,so let's say it's IE6

        webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";

       // DebugHelper.PrintHttpWebRequest(webRequest, TraceHelper.LineWithTrace(""));

        HttpWebResponse resp = webRequest.GetResponse() as HttpWebResponse;

        Stream strm = resp.GetResponseStream();

        return strm;//'HttpWebRequestHelper.GetResponseString(resp, sFileToSaveHtml);




Posted on Tuesday, March 13, 2007 2:47 PM | Back to top

Comments on this post: Explicitely specify Default user credentials for Web Proxy Server.

# re: Explicitely specify Default user credentials for Web Proxy Server.
Requesting Gravatar...
Is this code enough for calling webservice through MS ISA server. Please reply as i m stucking in a very difficult situation where i wanna call web service through ISA in window applications.

Left by Deepak on Jun 10, 2008 7:06 AM

# re: Explicitely specify Default user credentials for Web Proxy Server.
Requesting Gravatar...
From my expirience with MS ISA (not extensive) you don't need to write any code for ISA, but set configuration as described in my post "Set defaultProxy configuration Element for Proxy Server"
Left by Michael Freidgeim on Jun 10, 2008 9:16 AM

# re: Explicitely specify Default user credentials for Web Proxy Server.
Requesting Gravatar...
We have an enterprise windows application which is distributed among various client. As we are facing the problem for a particular client who is using MS ISA 2004, while the application is running in proxy environment, application is not able to hit our webservice which return the data to the application.
We have already implemented the proxy settings. The application is able to get the proxy URI but further communication request from the application is not received on server side.
We are using SoapHttpClientProtocol for calling the web service method.

// Creating the webservice object

MyService.MyContactsService objContactService = new MyService.MyContactsService();

// Creating CallWebServiceRequest object
CallWebServiceRequest objRequestManager= new CallWebServiceRequest();

// Calling WebMethods
MyContactsRecord contactsRecord = objRequestManager.MakeRequest(objContactService, "GetAllContacts", new object[]{contactID}) as MyContactsRecord ;

//In CallWebServiceRequest class

private SoapHttpClientProtocol myservice;

public object MakeRequest(SoapHttpClientProtocol webservice, string methodName, object[] parameters)
webservice.Timeout = Config.Timeout * 1000;
this.myservice = webservice;
this.methodName = methodName;
this.parameters = parameters;

Please help......
Left by Rahul on Jun 10, 2008 11:58 PM

# re: Explicitely specify Default user credentials for Web Proxy Server.
Requesting Gravatar...
ISA server has advanced logging capabilities, that allow to trace incomming, blocked and passed through requests.It should help you to find, why requests from your client could not reach web server.
You also can use FIDDLER on the client to confirm, that requests are send to ISA server.
Left by Michael Freidgeim on Jun 11, 2008 1:16 AM

# re: Explicitely specify Default user credentials for Web Proxy Server.
Requesting Gravatar...
I have been reading your blogs most of the day as I am trying to use an 2 app to send data through customer's proxy servers. I have not yet managed to get the data through a squid server as I always get 407 authentication errors. Is there no other way to have basic credentials in the header?
Left by gavin on Apr 01, 2009 12:11 AM

Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: