HttpWebResponse/HttpWebRequest problem...

  • Thread starter Satinderpal Singh
  • Start date
S

Satinderpal Singh

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="(e-mail address removed)",
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="(e-mail address removed)",
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
 
F

Feroze [MSFT]

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 said:
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="(e-mail address removed)",
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="(e-mail address removed)",
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
 
S

Satinderpal Singh

Thanks for response. Yeah i think, i'll try with CookieContainer, what
you have understood is absolutely correct.

We have to first try to login, so that it returns 401 - Unauthorized, on
this error, we have to set the cookie in the header and then carry on
subsequent transactions after logging in again.

Satinder


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top