473,890 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with HttpWebRequest/HttpWebResponse ...


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.Cre ate(new Uri(uri));

if(ParameterTab le.ContainsKey( "UserName") )
{
if(ParameterTab le.Contains("Pa ssword"))
{
request.Credent ials = new NetworkCredenti al ((String)Parame terTable["UserName"],(String)Parame terTable["Password"]);
}
}

if(ParameterTab le.ContainsKey( "Cookie"))
{
if (!requestParame ters.ContainsKe y("Cookie"))
{
requestParamete rs.Add("Cookie" ,ParameterTable["Cookie"]);
}
}

if(ParameterTab le.ContainsKey( "Authorization" ))
{
if (!requestParame ters.ContainsKe y("Authorizatio n"))
{
requestParamete rs.Add("Authori zation",Paramet erTable["Authorizat ion"]);
}
}
if(UserAgent!=n ull && UserAgent!="")
{
request.UserAge nt = UserAgent;
}
else
{
request.UserAge nt = "RETSExpres s/1.0";
}

HttpWebResponse wr = (HttpWebRespons e)request.GetRe sponse();
StreamReader sr = new StreamReader(wr .GetResponseStr eam());
string abc = sr.ReadToEnd();
return (HttpWebRespons e)request.GetRe sponse();

}

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="Se****@m etrolist.com", nonce="40f1e303 f5389bff1864ede e6c308795", opaque="c05d7e6 b44c97bf3d38cce e27f34b10b"
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_Session Id=zm0hcr55iia3 3jbtx4czxibj; 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="Se****@m etrolist.com", nonce="40f1e303 f5389bff1864ede e6c308795", opaque="c05d7e6 b44c97bf3d38cce e27f34b10b"
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_Session Id=ho1dj5vsbme0 l52q4hzun33f; 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
Nov 18 '05 #1
1 2806
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)

"Satinderpa l Singh" <sa**********@p lanetasia.com> wrote in message news:u3******** ******@TK2MSFTN GP12.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.Cre ate(new Uri(uri));

if(ParameterTab le.ContainsKey( "UserName") )
{
if(ParameterTab le.Contains("Pa ssword"))
{
request.Credent ials = new NetworkCredenti al ((String)Parame terTable["UserName"],(String)Parame terTable["Password"]);
}
}

if(ParameterTab le.ContainsKey( "Cookie"))
{
if (!requestParame ters.ContainsKe y("Cookie"))
{
requestParamete rs.Add("Cookie" ,ParameterTable["Cookie"]);
}
}

if(ParameterTab le.ContainsKey( "Authorization" ))
{
if (!requestParame ters.ContainsKe y("Authorizatio n"))
{
requestParamete rs.Add("Authori zation",Paramet erTable["Authorizat ion"]);
}
}
if(UserAgent!=n ull && UserAgent!="")
{
request.UserAge nt = UserAgent;
}
else
{
request.UserAge nt = "RETSExpres s/1.0";
}

HttpWebResponse wr = (HttpWebRespons e)request.GetRe sponse();
StreamReader sr = new StreamReader(wr .GetResponseStr eam());
string abc = sr.ReadToEnd();
return (HttpWebRespons e)request.GetRe sponse();

}

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="Se****@m etrolist.com", nonce="40f1e303 f5389bff1864ede e6c308795", opaque="c05d7e6 b44c97bf3d38cce e27f34b10b"
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_Session Id=zm0hcr55iia3 3jbtx4czxibj; 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="Se****@m etrolist.com", nonce="40f1e303 f5389bff1864ede e6c308795", opaque="c05d7e6 b44c97bf3d38cce e27f34b10b"
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_Session Id=ho1dj5vsbme0 l52q4hzun33f; 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
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3430
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not establish secure channel for SSL/TLS" private void mainHttpCalls(string postData) { HttpWebRequest objRequest1 ; HttpWebRequest objRequest2 ;
1
3231
by: Satinderpal Singh | last post by:
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...
22
3286
by: Kristof Thys | last post by:
Hello, I'm developing a C# - windows forms application. To get some information for my application, I'm connecting to an URL, wich gives me XML generated using php. With 90% of the users, this code just works fine: WebRequest request = WebRequest.Create(URL);
4
2293
by: octavio.filipe | last post by:
Hi, I'm trying to use the following code: HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest; webRequest.CookieContainer = new CookieContainer(); HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
6
5129
by: Georg | last post by:
Hello, I am trying to load a web page over a HTTP proxy with the POST method and I am using the following code: // open request (string url) HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); // set proxy
0
1112
by: davidpenty | last post by:
Hi there, I am having some problems with a multi-threaded asp.net seach page. My search page sends off four asynchronous http requests to four search engines then waits for the results to come back (in xml) The searches are just URLs with querystrings. Each request sets a boolean to true when it has got a reply from the search engine. I have declare the booleans as 'volatile'.
5
1571
by: Dhananjay | last post by:
hello everyone I have a problem with xml and sql server 2k. first thing i have a website named this-> http://en.infosites.org/info/keyword. when i am entering a value for keyword like this -> http://en.infosites.org/info/value now when this page opens in browser(IE or Mozila FF) it shows only xml tags with some text as contents. now i have to save this in sql server 2k i have a table in that i have field as xml tags. i want to save...
1
5692
by: ALA | last post by:
Hi, does anybody know if it is possible to pass the SessionID with a web request by using a cookie so that the invoked page in the same domain can access the session objects of the current user? HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = new CookieContainer(); HttpCookie sessionCookie = HttpContext.Current.Request.Cookies;
15
3106
by: Nightcrawler | last post by:
I am currently using the HttpWebRequest and HttpWebResponse to pull webpages down from a few urls. string url = "some url"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) {
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.