Catching HTTP error

S

Saya

Hi,

In my app I've done the following:
.....
wReq=(HttpWebRequest)WebRequest.Create(someURL);
.....
wRes=(HttpWebResponse)(wReq).GetResponse();

On this last line, the GetResponse generates a failure and my app
crashes with an exception error.
The question is: how can I catch this error? I've tried surrounding
the whole stuff with try-catch, but it doesn't enter my catch section!

From IExplorer I can see that they are able to do it: you just get a
blank page with a tiny red cross on it (instead of crashing ....).

Please advise; thank you.

Saya
 
M

Marina

It's not possible that it wouldn't go into the catch section if that is set
up to just catch Exception.

You need to show code.
 
V

victor

Hi Marina & Clint,

Here is the (offending ...) code snippet:
-------------------------------------------------
private Image GetImage(string sURL)
{
HttpWebRequest wReq;
HttpWebResponse wRes = null;
Stream str = null;

//Create a web request to the url containing the image
wReq = (HttpWebRequest)WebRequest.Create(sURL);

try
{
//gets the response from the web request
wRes = (HttpWebResponse)(wReq).GetResponse();

//return the image stream from URL specified earlier
str = wRes.GetResponseStream();
}
catch (WebException Ex)
{
MessageBox.Show(Ex.Message);
}

if (str != null)
{
imgImage = new System.Drawing.Bitmap(str);
str.Close();
wRes.Close();
return imgImage;
}
else
return null;
}
------------------------------------------------------------------------------------
Setting a brkpt at the catch line was to no avail ... after executing
the GetResponse line the system pops up an unhandled error and you can
only do a 'Details' or 'Quit' action. Details give just a mere silly
line of 'Main+0xf'.

Appreciate your help!
Saya
======================================================
 
M

Marina

Ok, that's in the debugger. It's trying to tell you as soon as there is a
problem.

If you just run the executable, it should work as expected.

If what you have is running in ASP.NET, then you won't see the message box
of course, since that would appear on the server, not in the client browser.
 
V

victor

OK, thanks. I'll look into it again tomorrow - it's quite late at my
place rigth now.
greetz
------------------------------------------------------------------------------------------
 
V

victor

Found it .... at last!
For what it's worth, the solution to my situation is: *not* using
'WebException' in the catch block, but use the plain 'Exception'!

Bit weird, but it works that way, it now enters my catch block when an
error turned up. Don't ask me why/how, I'm quite new to all these
stuff. Forgot to mention that it concerns a SmartDevice target running
under WinCE / CompactFramework.

victor
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top