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

Re: Read() on ResponseStream of a HttpWebRequest always returns zero bytes by Jon

Jon
Fri Mar 14 07:02:18 CDT 2008

Achim Domma <domma@procoders.net> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Re: Read() on ResponseStream of a HttpWebRequest always returns zero by Achim

Achim
Fri Mar 14 07:25:02 CDT 2008

On 14 Mrz., 13:02, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:

> 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

Re: Read() on ResponseStream of a HttpWebRequest always returns zero bytes by Jon

Jon
Fri Mar 14 08:25:00 CDT 2008

Achim Domma <domma@procoders.net> wrote:
> On 14 Mrz., 13:02, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
>
> > 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Re: Read() on ResponseStream of a HttpWebRequest always returns zero bytes by Barry

Barry
Fri Mar 14 08:27:15 CDT 2008

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 Domma wrote:

> On 14 Mrz., 13:02, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
>
> > 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:

> 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

--
http://barrkel.blogspot.com/

Re: Read() on ResponseStream of a HttpWebRequest always returns zero by Achim

Achim
Fri Mar 14 10:33:06 CDT 2008

On 14 Mrz., 14:25, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:

> 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