Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

My ASP.NET application calls web services( including Google Web API) and it is a requirement to access it through Proxy Server that requires Authentication. It works correctly with Microsoft ISA server (see my post Set defaultProxy configuration Element for Proxy Server) .

But it didn't work with Squid proxy server Authentication.

When I specified useDefaultCredentials=true, the WebException returned : “HTTP status 417: Unknown“.
After some investigation I found that 417 is actually Expectation failed and then that MS Web Services have HttpWebRequest and the Expect: 100-continue Header Problem .
So I had to override GetWebRequest for Web Services to set Expect100Continue = false and requests are going through Squid proxy server.Hurray!

using System.Net;

namespace Google_Web_APIs_Demo.Google

{

    public partial class GoogleSearchService//: System.Web.Services.Protocols.SoapHttpClientProtocol

    {

        protected override WebRequest GetWebRequest(Uri uri)

        {

           WebRequest wr= base.GetWebRequest(uri);

           HttpWebRequest wrHttp = wr as HttpWebRequest;

           if (wrHttp != null)

               wrHttp.ServicePoint.Expect100Continue = false;//avoid expects error http://haacked.com/archive/2004/05/15/449.aspx

           return wr;

        }

    }

}

 UPDATE: The behavior could be changed in config file:

 

<configuration>

<system.net>

<settings>

            <servicePointManager expect100Continue="false" />

</settings>

</system.net>

</configuration>

            

 UPDATE: Slightly related problem:
If you have error: "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF", add
add in the web.config the following (in brown)  (from http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=21106&SiteID=1&PageID=0 )

<configuration>
 <system.net>
 <settings>
 <httpWebRequest useUnsafeHeaderParsing="true" />
 </settings>
 </system.net>
 </configuration>

Update: the solution described above helped for one client with Squid, but din't help on another site.

Posted on Wednesday, September 13, 2006 11:06 AM ASP.NET , Web Services/WCF | Back to top


Comments on this post: Call Web Services through SQuid proxy server with authentication requested

# UseDefaultCredentials Problem with Web Service
Requesting Gravatar...
Hi there, I'm hoping you could maybe help me.

I've had a similar problem to you except when I use the code: UseDefaultCredentials = true; then I get an error: HTTP Status 401: Unauthorized. I searched like crazy for a solution to this and everywhere I look people say that UseDefaultCredentials is the Solution...but for me its the problem.

I know my problem has to do with credentials. The web service simply isn't receiving my credentials. My web service is accessed perfectly when I hardcode my credentials with this code:
MyWebService.Credentials = new System.Net.NetworkCredential("<MyUserName>", "<MyPassword>");

...but I of course don't want to hard code it. Why does UseDefaultCredentials not work for me? Is there some IIS setting or Web.Config details I need to change?

Any help will greatly be appreciated. Thank you.
Left by Jean on Nov 09, 2006 9:48 PM

# re: Call Web Services through SQuid proxy server with authentication requested
Requesting Gravatar...
Jean,
In my post http://geekswithblogs.net/mnf/archive/2006/03/08/71663.aspx I've describe how to "Change Identity of user account in Application Pool" to the account that is authorized on your network resource.
Hope this helps.

Michael.
Left by Michael Freidgeim on Nov 10, 2006 8:43 AM

# re: Call Web Services through SQuid proxy server with authentication requested
Requesting Gravatar...
I was also wondering if you could help me with a similar issue.

I have a .Net 2.0 application that connects with a company server across the Internet. I use Squid with NTLM authentication on my end, but the application uses different credentials. Thus, when I try to connect I get a 407 error. (By the way, I only have access to my client app and Squid.)

Is there a way to prefer the username and password entered into the .Net application to the NTLM authenication?

Thanking you in advance, Steve.
Left by Steve on Dec 12, 2006 7:41 PM

# re: Call Web Services through SQuid proxy server with authentication requested
Requesting Gravatar...
Steve,
In my post //geekswithblogs.net/mnf/archive/2006/03/08/71663.aspx I've described how to change user account for ASP.Net application. Another way is to use impersonation. You can do it in web.config or "Activate Windows Impersonation Selectively" http://www.ftponline.com/vsm/2004_05/magazine/columns/qa/default_pf.aspx
Left by Michael Freidgeim on Dec 13, 2006 7:35 AM

# re: Call Web Services through SQuid proxy server with authentication requested
Requesting Gravatar...
If you don't want to create a stub class just for grabbing a reference to the HttpWebRequest class, you could also use reflection to pluck the it from the SoapHttpClientProtocol object and tweak all the settings as needed...

// reflect to get the method info so we know how to invoke the protected method
var mi = typeof(SoapHttpClientProtocol).GetMethod("GetWebRequest", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Uri) }, null);

// "soapClient" is your web service object (.asmx class object):
var req = mi.Invoke(soapClient, new object[] { new Uri(soapClient.Url) }) as HttpWebRequest;

// then just set your proxy / other properties as needed
req.Proxy = WebRequest.DefaultWebProxy;
req.ServiceManager.Expect100Continue = false;
Left by Brock on Mar 12, 2010 1:30 AM

# re: Call Web Services through SQuid proxy server with authentication requested
Requesting Gravatar...
Take also a look there: http://www.squid-cache.org/Doc/config/ignore_expect_100/
Left by Thomas C. on Feb 05, 2012 5:51 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net