problem with HttpWebRequest/HttpWebResponse...

  • 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
 
B

bruce barker

because you filled in the creditals in advance, webrequest is automatically handling the 401 error. if you want to handle the 401, don't set any creditials until after you get the 401. after you get a 401, re-request with credentials.

-- bruce (sqlwork.com)




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
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top