WebClient timing out - how do I set longer timeout?

A

Arthur Yousif

Hello all,

I have an ASP.NET C# application that uses the WebClient class to retrieve a
report that's converted to PDF.

The URL passed to the WebClient object gets the PDF stream. It works great
except for some reports that exceed a timeout value of some type.

I've been unable to figure out how to set a timeout value for the WebClient
object so it waits for longer than the default which seems to be around 1m
45s.

I appreciate any help you can provide as I'm really stuck right now.

My code is very simple:


code:-----------------------------------------------------------------------
 
N

Nicholas Paldino [.NET/C# MVP]

Arthur,

Instead of using the WebClient class, I would recommend using the
HttpWebRequest and HttpWebResponse classes. It might require a little more
work, but you have fine-grained control over things like the timeout, which
can be accessed through the Timeout property on the HttpWebRequest class.

Hope this helps.
 
A

Arthur Yousif

Hi Nicholas,

Thanks for your response. I had started by using the controls you
recommend, however, I was unable to get the stream in binary format like I
need to. The characters in the stream were always changed because I had to
use the Char or String objects which "broke" the binary stream. If you know
how to extract the stream in binary format, please help me out as I was
never able to do so.

I would prefer to use the HttpWebRequest as well if I can get what I need.
Thanks much.

--
Arthur


Nicholas Paldino said:
Arthur,

Instead of using the WebClient class, I would recommend using the
HttpWebRequest and HttpWebResponse classes. It might require a little more
work, but you have fine-grained control over things like the timeout, which
can be accessed through the Timeout property on the HttpWebRequest class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arthur Yousif said:
Hello all,

I have an ASP.NET C# application that uses the WebClient class to
retrieve
a
report that's converted to PDF.

The URL passed to the WebClient object gets the PDF stream. It works great
except for some reports that exceed a timeout value of some type.

I've been unable to figure out how to set a timeout value for the WebClient
object so it waits for longer than the default which seems to be around 1m
45s.

I appreciate any help you can provide as I'm really stuck right now.

My code is very simple:
code:-----------------------------------------------------------------------
 
J

Joerg Jooss

Arthur said:
Hi Nicholas,

Thanks for your response. I had started by using the controls you
recommend, however, I was unable to get the stream in binary format
like I need to. The characters in the stream were always changed
because I had to use the Char or String objects which "broke" the
binary stream. If you know how to extract the stream in binary
format, please help me out as I was never able to do so.

I would prefer to use the HttpWebRequest as well if I can get what I
need. Thanks much.

Streams only deliver binary data, i.e. bytes. You're obviously doing
something wrong here. Post your code.

Cheers,
 
A

Arthur Yousif

Hi Joerg,

That's interesting, because when I used the stream method, my PDF stream was
corrupt and the resulting file was not correctly formatted. When I use the
current WebClient method it works fine.

Unfortuneately, I no longer have the code as I replaced it with the
WebClient code a long time ago. I'd have to rewrite it, but I figure what's
the point if I couldn't get it working. Keep in mind that I was getting the
data stream just fine, it was just not the correct binary data or it was
being altered somehow. I need it in Byte array format and not Char array
which each hold 1 byte and 2 bytes respectively.

Do you have a quick example that reads an HttpRequest response into a Byte[]
array w/out doing a Char conversion? Because that's what I had to do which
"broke" the data streams content.

Thanks much.
 
A

Arthur Yousif

I think I found the problem. I remember when I initially used this
methodology, I read the Stream into a StreamReader. I just realized after
revisiting the documentation, that I really should be accessing the Stream
object directly and using either the Read or ReadByte methods.

I think this will work once I'm done because, you're right, the data will be
in binary format. Thanks much.
 
D

Dilip Krishnan

Looks like you would need to use a ByteReader as opposed to a String
reader. I guess the encoded bytes are lost in conversion to char or string
Hope that helps
 

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

Top