Re: HttpWebResponse/HttpWebRequest problem...

From: Feroze [MSFT] (ferozed_at_online.microsoft.com)
Date: 06/03/04


Date: Thu, 3 Jun 2004 14:12:21 -0700

The way authentication works is : you send a request to the server. If the
server wants to authenticate you, it will reply wiht a 401. Then the client
sends another request with credentials, and if they creds are correct, the
server would respond with 200 (or another 401 if the creds are wrong).

from what I understand, you want to:

a) authenticate with the server using digest auth + credentials.
b) after successful auth, server sends you a cookie.
c) on subsequent requests, you want to use the cookie to access the server.

If this is correct, you just need to set a CookieContainer on the
webrequest. This container will contain all cookies sent by the server. The
cookies will be sent automatically on subsequent request.

-- 
feroze
http://weblogs.asp.net/feroze_daud
============
Remove "user" from the email address to reply to the author.
This posting is provided "AS IS" with no warranties, and confers no rights
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Satinderpal Singh" <satinderpals@planetasia.com> wrote in message
news:OqqHzQISEHA.3944@tk2msftngp13.phx.gbl...
>
> Hi everyone,
>
> We are using HttpWebRequest to create a request to a URI, which requires
us
> to login first. In order to process all the transactions, first we have to
> login and get the cookie value in a variable and make the request again
with
> that cookie as a header value (Digest Authentication).
> But, the problem we are facing is: That the HttpWebRequest is getting two
> responses simultaneously, which means that the code should wait after the
> first response is received, throw an error, so that we can take the cookie
> value and make a request again, but, its not working that way, the request
> objects gets two values as response (we watched it through Packet
Sniffer).
> Packet Sniffer shows two responses received, the first one is 401 and the
> second one is the successful one. Ideally it should break the code after
> 401, but it doesn't, any clue as to why it doesn't waits after the first
> response ? Could there be any problem with the server ? Or anything wrong
> with our code (mentioned below).
>
> The server (ASPX URL) we are trying to connect has .NET Framework 1.1 and
> Http/1.1
>
> Following is the code we are using to get the response object from the
> request:
>
> HttpWebRequest request = null; // Holds the instance of Http request
>
> try
>
> {
>     request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
>
>     if(ParameterTable.ContainsKey("UserName"))
>     {
>         if(ParameterTable.Contains("Password"))
>         {
>             request.Credentials = new NetworkCredential
> ((String)ParameterTable["UserName"],(String)ParameterTable["Password"]);
>         }
>     }
>
>     if(ParameterTable.ContainsKey("Cookie"))
>     {
>         if (!requestParameters.ContainsKey("Cookie"))
>         {
>             requestParameters.Add("Cookie",ParameterTable["Cookie"]);
>         }
>     }
>
>     if(ParameterTable.ContainsKey("Authorization"))
>     {
>         if (!requestParameters.ContainsKey("Authorization"))
>         {
>
> requestParameters.Add("Authorization",ParameterTable["Authorization"]);
>         }
>     }
>
>
>     if(UserAgent!=null && UserAgent!="")
>     {
>         request.UserAgent = UserAgent;
>     }
>     else
>     {
>         request.UserAgent = "RETSExpress/1.0";
>     }
>
>
>
>     HttpWebResponse wr = (HttpWebResponse)request.GetResponse();
>      StreamReader sr = new StreamReader(wr.GetResponseStream());
>     string abc = sr.ReadToEnd();
>     return (HttpWebResponse)request.GetResponse();
>
> }
>
> catch.... part...
>
> In the above code either the value of abc should be both the responses, or
> it should break after 401 and get in to the catch part. This code works
> smooth with most of the URIs, its only one URI which is giving problem.
>
> Responses Received:
> --------------------------------------------------------------------------
--
> --------------------------------------------------------------------------
--
> --------------------------------------------------------
> HTTP/1.1 401 Unauthorized
> Server: Microsoft-IIS/5.0
> Date: Wed, 02 Jun 2004 09:20:18 GMT
> X-Powered-By: ASP.NET
> X-AspNet-Version: 1.1.4322
> WWW-Authenticate: Digest qop="auth", realm="Server@metrolist.com",
> nonce="40f1e303f5389bff1864edee6c308795",
> opaque="c05d7e6b44c97bf3d38ccee27f34b10b"
> RETS-Version: RETS/1.5
> Local Time: 6/2/2004 3:20:17 AM
> Request client: 203.200.29.119
> Error: NO_NONCE
> Set-Cookie: ASP.NET_SessionId=zm0hcr55iia33jbtx4czxibj; path=/
> Set-Cookie: RETS-Session-ID=RETSSESS-252f7f30-d2ab-4944-847b-ed8f207c2628;
> path=/
> Cache-Control: private
> Content-Type: text/xml
> --------------------------------------------------------------------------
--
> --------------------------------------------------------------------------
--
> --------------------------------------------------------
>
> Immediately after the above response, the next response it shows in Packet
> Sniffer is:
>
> --------------------------------------------------------------------------
--
> --------------------------------------------------------------------------
--
> --------------------------------------------------------
> HTTP/1.1 200 OK
> Server: Microsoft-IIS/5.0
> Date: Wed, 02 Jun 2004 09:20:25 GMT
> X-Powered-By: ASP.NET
> X-AspNet-Version: 1.1.4322
> WWW-Authenticate: Digest qop="auth", realm="Server@metrolist.com",
> nonce="40f1e303f5389bff1864edee6c308795",
> opaque="c05d7e6b44c97bf3d38ccee27f34b10b"
> RETS-Version: RETS/1.5
> Local Time: 6/2/2004 3:20:24 AM
> Request client: 203.200.29.119
> Error:
> Transfer-Encoding: chunked
> Set-Cookie: ASP.NET_SessionId=ho1dj5vsbme0l52q4hzun33f; path=/
> Cache-Control: private
> Content-Type: text/xml; charset=utf-8
>
>
> Though it should have returned back to the code after the 401 unauthorized
> error as a WebException, which works fine with all the other URIs.
>
> Any help would be highly appreciated. Sorry for a long description, but we
> thought it would make sense to put as much info. as possible.
>
> Thanks
> Satinder
>
>


Relevant Pages

  • Re: NotificationSampleWebDav-Monitor OWA Inbox
    ... re-login or update your cookie with each response you get... ... Warning: Exiting Action with an exception: The remote server returned an ... // Create request object and assign credentials. ... Stream newStream = Request.GetRequestStream; ...
    (microsoft.public.exchange.applications)
  • Re: Problems with access to a web page
    ... Server: Apache ... Connection: close ... Look what I get now when I send the exact same request ... and got exactly the same 0 length response ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: BASIC authentication Issues with IE - Part II - Solved but WHY?
    ... REQUEST and RESPONSE logging below to show you whats going on. ... using my local machine web server. ... However, at this point I am logged in and I have lots of links, one is a "who is online" link client?who.wcn, which I will open this up in a second window. ...
    (microsoft.public.inetserver.iis.security)
  • Re: NotificationSampleWebDav-Monitor OWA Inbox
    ... when you send your authentication cookies along a request in the ... the response should also contain new cookies. ... Can you guide me how can I update the cookie with new response. ... Warning: Exiting Action with an exception: The remote server returned ...
    (microsoft.public.exchange.applications)
  • Re: HTTP header processing sequence
    ... Initially it sends the request for the html source, and if there are any more objects to request it does so, and then wait for the server to reply sending back the requested data using appropriate headers. ... How does the (browser) client know how to match up multiple received responses from the server with GET-requests, as this does not appear to be sequential. ... So the association of request and response is no problem. ...
    (microsoft.public.win32.programmer.networks)