401 Error using HttpWebRequest in .NET

Ryan.Melena_at_gmail.com
Date: 04/27/05


Date: 27 Apr 2005 08:26:45 -0700

Hello all,

I'm experiencing a very sporadic error since our company upgraded our
intranet server to IIS6 (win2k3 sp1). The problem is stemming from an
HttpWebRequest made in one of my .NET applications which is attempting
to include the contents of a web page in an email. I have attempted
sending credentials in different ways as shown below (both of which
worked without problem on IIS5 and win2k):

<CODE>
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.PreAuthenticate = true;

// Way 1:
webRequest.Credentials = CredentialCache.DefaultCredentials;

// Way 2:
webRequest.Credentials = new
NetworkCredential("username","password","domain");

HttpWebResponse webResponse =
(HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
</CODE>

In both cases many requests are successful and the e-mail is sent
without a hitch. Other times, however, the request fails with a 401
error and an exception is thrown. Examples of the log file for each
case are shown below (HttpWebRequests followed by POST of page making
the request).

<SUCCESS>
2005-04-27 13:32:51 172.22.2.42 GET
/Apps/JobCreate/jobdetails_popup.aspx jobid=3030 80 - 172.22.2.42 - 401
2 2148074254
2005-04-27 13:32:51 172.22.2.42 GET
/Apps/JobCreate/jobdetails_popup.aspx jobid=3030 80 EGAN\rjm
172.22.2.42 - 200 0 0
2005-04-27 13:32:51 172.22.2.42 POST /Apps/JobCreate/index.aspx - 80
EGAN\gdp 172.22.2.104
Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.0;+.NET+CLR+1.1.4322)
200 0 0
</SUCCESS>

<FAILURE>
2005-04-27 14:58:22 172.22.2.42 GET
/Apps/JobCreate/jobdetails_popup.aspx jobid=3033 80 - 172.22.2.42 - 401
1 0
2005-04-27 14:58:22 172.22.2.42 GET
/Apps/JobCreate/jobdetails_popup.aspx jobid=3033 80 - 172.22.2.42 - 401
1 2148074252
2005-04-27 14:58:22 172.22.2.42 POST /Apps/JobCreate/index.aspx - 80
EGAN\sks 172.22.4.116
Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.0) 200 0 0
</FAILUR>

As can be seen from the log file, the same page is being requested in
every case (with only a different parameter in the URL). There are no
errors on the page being requested as it can be visited directly
without a problem.

The IIS Application is set to allow Integrated Windows Authentication
only (because I need to grab the user's login name for other purposes).

The problem does not seem to be linked to any particular user or
computer which is the thing that is really confusing me. It seems to
happen almost at random.

Any help that anyone could offer would be greatly apprecaited!

Thank you,
Ryan



Relevant Pages

  • Re: Internet/Web classes in .NET/VC++...
    ... open a child window and request the result of the query from another page ... If I use the C++ WebClient class, I can successfully get the end results. ... queries, I cannot use the name-value form for sending data. ... So, I tried the HTTPWebRequest class (inherited from WebRequest, specific ...
    (microsoft.public.dotnet.languages.vc)
  • Re: forms auth, authenticate against already encrypted password?
    ... i actually tried using a WebClient implementation first, sending a POST request to login.aspx which handled Request.Formetc in the Page_Load event. ... i have tried it again using the HttpWebRequest class and now i can share the CookieContainers, ... programamtically send request to forms authentication protected webpage. ... you can consider share a cookie container ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: WebResponse.GetResponseStream() exception
    ... programmatically request some web pages, ... Does this problem frequently occur when you use HttpWebRequest class to ... here is a test program which can programmatically request the ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework)
  • Re: Is it possible to turn off "HTTP Keep-Alive" globally in app.config?
    ... If the server hosting the Web Service is under your control can't you just ... getting the ConstructorInfo for HttpWebRequest, ... instance make new TCP connection for each web service request. ... SoapHttpClientProtocol has a protected method GetWebRequest(System.Uri ...
    (microsoft.public.dotnet.languages.csharp)
  • True asynchronous HttpWebRequest implementation with abort and timeout
    ... I'm tired of seeing all these asynchronous HttpWebRequest ... How do I implement an cancel method that aborts the request? ... private static void RespCallback(IAsyncResult asynchronousResult) ... // Read the response into a Stream object. ...
    (microsoft.public.dotnet.framework)