Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trapping web return errors
Message
De
19/12/2014 05:14:19
 
 
À
18/12/2014 12:25:18
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows Server 2008
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01612403
Message ID:
01612433
Vues:
53
>>>>When I call a webservice and it returns an error how can I trap the full error message
>>>>
>>>>I've got this code
>>>>
>>>>HttpWebResponse response = null;
>>>> HttpWebRequest request = (HttpWebRequest)WebRequest.Create(profileAPIURL);
>>>> request.Headers.Add("Authorization", oAuth);
>>>> try
>>>> {
>>>> response = (HttpWebResponse)request.GetResponse();
>>>> }
>>>> catch (Exception ex)
>>>> {
>>>> Console.WriteLine("Exception Occured:" + ex.Message);
>>>> }
>>>>
>>>>but the catch just gives me a server error like "The remote server returned an error: (400) Bad Request." and I want to know the reason its a bad request.
>>>>
>>>>Thanks
>>>>
>>>>Nick
>>>
>>>400 should mean that your request was badly/wrongly formed
>>>
>>>You could just use Fiddler - that will show you the exact request that was sent and full response received from the server.
>>>
>>>You can also use fiddler to tinker with the request parameters/headers until you get a valid response.
>>
>>Thanks Viv
>>
>>v keen on fiddler I see :-)
>
>Worth it's weight in gold (OK - software doesn't weigh much :-} )
>
>>
>> but I was thinking of how I'd trap errors in a running process.
>>
>>I'm running some tests through the webservices demo api interface and through c#. the demo webservice api interface gives me an error message and a server response number (400) but in c# I only see the 400 error. Should I remove the try catch and manually handle the response.
>
>Not sure what you mean by the 'webservices demo api interface' but if there's nothing there that actually deals with the exception then why not let it flow up to your own exception handler - it should still have all the details.....

Concur have a test site with an interface that lets you fill in and send POST and GETS to their API.
the response = (HttpWebResponse)request.GetResponse(); throws an error throws an error into ex catch (Exception ex) but that doesn't seem to have all the error detail that displays on the test |I mentioned above.So you mean take out the try catch ?

Update

WebException wex = (WebException)ex;
var s = wex.Response.GetResponseStream();
StreamReader reader = new StreamReader(s);
string fileInfo = reader.ReadToEnd();

Gives me the full response that I can use in the catch block
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform