Read() on ResponseStream of a HttpWebRequest always returns zerobytes

  • Thread starter Thread starter Achim Domma
  • Start date Start date
A

Achim Domma

Hi,

I have to check if a url like http://my_server/abc.aspx?id=123 sends a
valid file. The server always returns OK as status code, but sends
emtpy files, if the id is not correct. It's not possible for me to
change the server side. Content-Lenght is always zero, because files
are in a streaming format (I think).

I tried to read the first 10 bytes of the file to check if it contains
some valid data, but Read(...) returns zero bytes even for the valid
ids. If I try the same url in IE, I'm able to download the file.

Any hint what I might be doing wrong? Or another way to check the
files?

regards,
Achim
 
Achim Domma said:
I have to check if a url like http://my_server/abc.aspx?id=123 sends a
valid file. The server always returns OK as status code, but sends
emtpy files, if the id is not correct. It's not possible for me to
change the server side. Content-Lenght is always zero, because files
are in a streaming format (I think).

I tried to read the first 10 bytes of the file to check if it contains
some valid data, but Read(...) returns zero bytes even for the valid
ids. If I try the same url in IE, I'm able to download the file.

Any hint what I might be doing wrong? Or another way to check the
files?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Could you post a short but complete program which demonstrates the
problem?

I could not publish the original URL I'm downloading from, but here is
my test code:

WebRequest req =
WebRequest.Create("http://some_url/dummy.mp3?moduleId=1234");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Console.WriteLine("Content-Lenght: {0}",resp.ContentLength);
Console.WriteLine("Content-Type: {0}", resp.ContentType);

byte[] buffer = new byte[10];
Stream stream = resp.GetResponseStream();
int bytesRead = stream.Read(buffer, 0, 10);
if (bytesRead>0)
{
Console.WriteLine("file is ok");
}
else
{
Console.WriteLine("bad file");
}

The output is always the same, even for urls which can be downloaded
via IE:

Content-Lenght: 0
Content-Type: audio/x-mpeg;charset=UTF-8
bad file

best regards,
Achim
 
Achim Domma said:
Could you post a short but complete program which demonstrates the
problem?

I could not publish the original URL I'm downloading from, but here is
my test code:

WebRequest req =
WebRequest.Create("http://some_url/dummy.mp3?moduleId=1234");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Console.WriteLine("Content-Lenght: {0}",resp.ContentLength);
Console.WriteLine("Content-Type: {0}", resp.ContentType);

byte[] buffer = new byte[10];
Stream stream = resp.GetResponseStream();
int bytesRead = stream.Read(buffer, 0, 10);
if (bytesRead>0)
{
Console.WriteLine("file is ok");
}
else
{
Console.WriteLine("bad file");
}

The output is always the same, even for urls which can be downloaded
via IE:

Content-Lenght: 0
Content-Type: audio/x-mpeg;charset=UTF-8
bad file

Welll, notice that you've got a content length of 0. It's entirely
reasonable that the stream doesn't contain any data!

It's likely that the server is doing something like User-Agent
checking, and not returning the data.
 
Here's a short and complete program that runs, but it doesn't show your
problem:

---8<---
using System;
using System.Net;
using System.IO;

namespace WebReq
{
class Program
{
static void Main(string[] args)
{
WebRequest req =
WebRequest.Create("http://example.com/");
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

Console.WriteLine("Content-Lenght: {0}",resp.ContentLength);
Console.WriteLine("Content-Type: {0}", resp.ContentType);

byte[] buffer = new byte[10];
Stream stream = resp.GetResponseStream();
int bytesRead = stream.Read(buffer, 0, 10);
if (bytesRead>0)
{
Console.WriteLine("file is ok");
}
else
{
Console.WriteLine("bad file");
}
}
}
}
--->8---

I replaced the URL so that it would run. It would appear the problem is
in the URL. Can you find a different URL which reproduces the problem,
and show us it?

Achim said:
I could not publish the original URL I'm downloading from, but here is
my test code:
The output is always the same, even for urls which can be downloaded
via IE:

With the above program (not an mp3 download) I get:

Content-Lenght: 438
Content-Type: text/html; charset=UTF-8
file is ok

-- Barry
 
It's likely that the server is doing something like User-Agent
checking, and not returning the data.

Yes, setting the User-Agent header to the value passed by IE solved
the problem! Thanks a lot for the hint!

best regards,
Achim
 
Back
Top