473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Empty HttpWebResponse length for some url (with queries?)

For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #1
13 9772
Hi,

You are using this:

StreamReader sr = new StreamReader(resp.GetResponseStream());

Shouldn't you be using this:

StreamReader sr = new StreamReader(resp.GetResponse().GetResponseStream( ));

The rest seens fine to me!
Hope it helps...
o.f

Nov 17 '05 #2

Is the length empty or is the object empty? Streams from webrequests by
default dont allow seeking so it won't know the length off hand.. if you
put a break in the code after it gets the stream you should be able to
hover over the stream and see if it's null or not, it shouldn't be null
unless it got an error, but it may not know the length...

Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #3
The object is not empty. The length of pageData (created with
StreamReader.readToEnd()) is 0. And the resp.ContentLength is also 0. But it
does show info like ContentType (which is "text/html; charset=iso-8859-1").
The HttpStatusCode is also OK. This problem occurs for only this URL. Is
there a special requirement in the WebRequest for URLs with query strings in
it?

"Michael McCarthy" wrote:

Is the length empty or is the object empty? Streams from webrequests by
default dont allow seeking so it won't know the length off hand.. if you
put a break in the code after it gets the stream you should be able to
hover over the stream and see if it's null or not, it shouldn't be null
unless it got an error, but it may not know the length...

Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #4
I also found in the response header info like:

Set-Cookie: JSESSIONID=0000AQWFK7gpN1TDbm0IKxJ94Tu:10dg77lr6;P ath=/

"Jason Manfield" wrote:
The object is not empty. The length of pageData (created with
StreamReader.readToEnd()) is 0. And the resp.ContentLength is also 0. But it
does show info like ContentType (which is "text/html; charset=iso-8859-1").
The HttpStatusCode is also OK. This problem occurs for only this URL. Is
there a special requirement in the WebRequest for URLs with query strings in
it?

"Michael McCarthy" wrote:

Is the length empty or is the object empty? Streams from webrequests by
default dont allow seeking so it won't know the length off hand.. if you
put a break in the code after it gets the stream you should be able to
hover over the stream and see if it's null or not, it shouldn't be null
unless it got an error, but it may not know the length...

Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #5
Probably you need to send that cookie on the webrequest you are doing.

o.f
Nov 17 '05 #6
Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...5028634&F=0&QP
N=WO2005028634), the content length for the HttpWebResponse I get
with request.GetResponse in empty. The response.GetResponseStream()
also empty. However, I am able to open the URL in the browser (the
URL address remains the same; it is not redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1"
and the HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?


The character encoding for example. You're using a UTF-8 StreamReader
to read ISO-8859-1 which will cause problems. Create a ISO-8859-1
StreamReader to decode the response properly:

Encoding enc = Encoding.GetEncoding(28591);
StreamReader reader = new StreamReader(reponseStream, enc);

But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite possible
that this breaks HttpWebResponse.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #7
I looked at this in some detail. I think the server is looking for a
User-Agent header in the request. If it finds one, it sends a response page.
Otherwise it doesnt.

You can take a look at the attached program. Set the proxy if needed. Run
the program with the UserAgent set. You will see the page getting
downloaded. Then rerun it with that line commented out. And the response
will have Zero content-length.


using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class EP {
public static void Main(string [] args)
{
string us =
"http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO20050 28634";
HttpWebRequest req = WebRequest.Create(us) as HttpWebRequest;
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.6)
Gecko/20";
//req.Proxy = new WebProxy("http://my-proxy");

try {
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

foreach(string h in resp.Headers) {
Console.WriteLine(h + ": " + resp.Headers[h]);
}

Stream rs = resp.GetResponseStream();
MemoryStream ms = new MemoryStream();

byte [] data = new byte[1024];
int read = rs.Read(data,0,data.Length);
while(read > 0) {
ms.Write(data,0,read);
read = rs.Read(data,0,data.Length);
}

resp.Close();

ms.Seek(0,SeekOrigin.Begin);

StreamReader sr = new StreamReader(ms);
String s = sr.ReadToEnd();
sr.Close();

Console.WriteLine("====");
Console.WriteLine(s);

} catch(Exception e) {
Console.WriteLine(e);
}
}
}

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

"Jason Manfield" <Ja***********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse
in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and
the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #8
I looked at this in some detail. I think the server is looking for a
User-Agent header in the request. If it finds one, it sends a response page.
Otherwise it doesnt.

You can take a look at the attached program. Set the proxy if needed. Run
the program with the UserAgent set. You will see the page getting
downloaded. Then rerun it with that line commented out. And the response
will have Zero content-length.


using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class EP {
public static void Main(string [] args)
{
string us =
"http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO20050 28634";
HttpWebRequest req = WebRequest.Create(us) as HttpWebRequest;
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.6)
Gecko/20";
//req.Proxy = new WebProxy("http://my-proxy");

try {
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

foreach(string h in resp.Headers) {
Console.WriteLine(h + ": " + resp.Headers[h]);
}

Stream rs = resp.GetResponseStream();
MemoryStream ms = new MemoryStream();

byte [] data = new byte[1024];
int read = rs.Read(data,0,data.Length);
while(read > 0) {
ms.Write(data,0,read);
read = rs.Read(data,0,data.Length);
}

resp.Close();

ms.Seek(0,SeekOrigin.Begin);

StreamReader sr = new StreamReader(ms);
String s = sr.ReadToEnd();
sr.Close();

Console.WriteLine("====");
Console.WriteLine(s);

} catch(Exception e) {
Console.WriteLine(e);
}
}
}

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

"Jason Manfield" <Ja***********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...=WO2005028634),
the content length for the HttpWebResponse I get with request.GetResponse
in
empty. The response.GetResponseStream() also empty. However, I am able to
open the URL in the browser (the URL address remains the same; it is not
redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1" and
the
HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?

Thanks

Jason

Nov 17 '05 #9
Realized (by experimenting) that setting the UserAgent in HttpWebRequest
solved the problem. But, hey, I could access most of websites without setting
it.

req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
resp = (HttpWebResponse)req.GetResponse();

"Joerg Jooss" wrote:
Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...5028634&F=0&QP
N=WO2005028634), the content length for the HttpWebResponse I get
with request.GetResponse in empty. The response.GetResponseStream()
also empty. However, I am able to open the URL in the browser (the
URL address remains the same; it is not redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1"
and the HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?


The character encoding for example. You're using a UTF-8 StreamReader
to read ISO-8859-1 which will cause problems. Create a ISO-8859-1
StreamReader to decode the response properly:

Encoding enc = Encoding.GetEncoding(28591);
StreamReader reader = new StreamReader(reponseStream, enc);

But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite possible
that this breaks HttpWebResponse.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 17 '05 #10
Realized (by experimenting) that setting the UserAgent in HttpWebRequest
solved the problem. But, hey, I could access most of websites without setting
it.

req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
resp = (HttpWebResponse)req.GetResponse();

"Joerg Jooss" wrote:
Jason Manfield wrote:
For some URLs
(e.g.http://v3.espacenet.com/origdoc?DB=E...5028634&F=0&QP
N=WO2005028634), the content length for the HttpWebResponse I get
with request.GetResponse in empty. The response.GetResponseStream()
also empty. However, I am able to open the URL in the browser (the
URL address remains the same; it is not redirected)

Here is the code snippet:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(pageAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string pageData = sr.ReadToEnd();

The Content Type for the response is "text/html; charset=iso-8859-1"
and the HttpStatusCode was OK. The pageData length is 0.

Should the URL be modified for it to work (e.g. some substitution)?

What am I missing?


The character encoding for example. You're using a UTF-8 StreamReader
to read ISO-8859-1 which will cause problems. Create a ISO-8859-1
StreamReader to decode the response properly:

Encoding enc = Encoding.GetEncoding(28591);
StreamReader reader = new StreamReader(reponseStream, enc);

But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite possible
that this breaks HttpWebResponse.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 17 '05 #11
Joerg Jooss wrote:
But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite possible
that this breaks HttpWebResponse.


But the content length would only be a specific length when using keep
alive with http 1.1+... which we can assume, using .NET that we don't
care about, since we can use a common connection pool that deprecates
keep alive.

think so?

~Michael.
Nov 17 '05 #12
Joerg Jooss wrote:
But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite possible
that this breaks HttpWebResponse.


But the content length would only be a specific length when using keep
alive with http 1.1+... which we can assume, using .NET that we don't
care about, since we can use a common connection pool that deprecates
keep alive.

think so?

~Michael.
Nov 17 '05 #13
Michael McCarthy wrote:
Joerg Jooss wrote:
But you're right about the Content-Length: The server sends
"Content-Length: 0", which is a disastrous error. It is quite
possible that this breaks HttpWebResponse.


But the content length would only be a specific length when using
keep alive with http 1.1+... which we can assume, using .NET that we
don't care about, since we can use a common connection pool that
deprecates keep alive.

think so?


In HTTP 1.1 you either

a) specify a Content-Length
b) use Transfer-Encoding: chunked
c) use Connection: close

using c) as request header disables the use of persistent connections
("keep-alive" is an outdated HTTP 1.0+ term).

I don't get the part with the connecion pooling. A connection pool only
makes sense to, um, pool connections to a well known end communication
end points, e.g. a database or an EIS. HTTP clients in general do
communicate with a lot of *different* end points, so what exactly do
want to pool? And regardless of pooling, "Connection: close" must
terminate the TCP connection:

"Once a close has been signaled, the client MUST NOT send any more
requests on that connection." (§8.1.2)

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #14

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

Similar topics

0
by: Denny Rue | last post by:
I’ve created VB code to download files from a web site through the use of HTTPWebRequest, HTTPWebResponse and BinaryReader. The HTTPWebRequest has a TimeOut property to limit how long it waits...
0
by: Michael Evanchik | last post by:
Hello all, since i wanted to use ssl and its seems easy to do so with this object. Im trying to login to a webserver (aol) for this example. But for some reason, im packet sniffing with ethreal...
0
by: Nathan | last post by:
This is a copy of a message at microsoft.public.dotnet.framework.clr: THE CODE: I'm using an HttpWebResponse object to send an HTTP POST to a Java server I have written and are running on the...
1
by: Ravi | last post by:
Hi , i am trying to pass the same session Id to all the webrequest, but sometimes the response.cookies returns zero and sometimes one. is this to do something with cookies expire. In this sample...
3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
3
by: Sean Chapman | last post by:
ok, heres the problem.. i have an asp.net page that im using to kind of relay information back and forth. So on the Page_Load i make a request to a webservice and return some xml back to the first...
2
by: Nuno Magalhaes | last post by:
In pages that there is no content length, how does HttpWebResponse knows where the page ends? And what kind of objects/methods does it retrieve? Does it only retrieve the initial page without any...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
4
by: JVNewbie | last post by:
I'm attempting to test an aspx module that will receive XML data from a web service (the receiver module). I want to be able to test this portion before attempting to create the Web Service that will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.