How to handle errors for HTTPWebRequest.GetResponse call?

azharsaeedkhan
Member Posts: 37
When HTTPWebRequest.GetResponse is called and no proper response is returned from the server, the code stops executing and returns some random error. I want to implement proper error handling, so the code keeps on executing without stopping here and a proper message is displayed to the user. Like we apply checks on status code when we use automation objects.
Any help in this regard would be appreciated.
Any help in this regard would be appreciated.
0
Best Answer
-
Hello @azharsaeedkhan
In NAV 2013 that don't have a try function I do something like thisASSERTERROR BEGIN Response := Request.GetResponse(); IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN ResponseStream := Response.GetResponseStream(); ERROR('OK'); END ELSE BEGIN HttpWebStatus := Response.StatusCode; ERROR(Response.StatusDescription); END; END; IF GETLASTERRORTEXT = 'OK' THEN BEGIN IF NOT ISNULL(ResponseStream) THEN BEGIN .....
Regards.1
Answers
-
The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
Pseudo code:
IF NOT HttpWebRequest.GetResponse THEN ERROR('')
I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
Pseudo code:
if HttpStatusCode = 200 THEN
EXIT(True) ELSE // Success
ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
0 -
> @irasoelbaks said:
> The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
>
> Pseudo code:
> IF NOT HttpWebRequest.GetResponse THEN ERROR('')
>
> I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
>
> Pseudo code:
> if HttpStatusCode = 200 THEN
> EXIT(True) ELSE // Success
> ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
It doesn't return a boolean, but HttpWebResponse object.
The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.0 -
Hello @azharsaeedkhan
In NAV 2013 that don't have a try function I do something like thisASSERTERROR BEGIN Response := Request.GetResponse(); IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN ResponseStream := Response.GetResponseStream(); ERROR('OK'); END ELSE BEGIN HttpWebStatus := Response.StatusCode; ERROR(Response.StatusDescription); END; END; IF GETLASTERRORTEXT = 'OK' THEN BEGIN IF NOT ISNULL(ResponseStream) THEN BEGIN .....
Regards.1 -
azharsaeedkhan wrote: »> @irasoelbaks said:
> The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
>
> Pseudo code:
> IF NOT HttpWebRequest.GetResponse THEN ERROR('')
>
> I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
>
> Pseudo code:
> if HttpStatusCode = 200 THEN
> EXIT(True) ELSE // Success
> ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
It doesn't return a boolean, but HttpWebResponse object.
The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
0 -
Hello @azharsaeedkhan
In NAV 2013 that don't have a try function I do something like thisASSERTERROR BEGIN Response := Request.GetResponse(); IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN ResponseStream := Response.GetResponseStream(); ERROR('OK'); END ELSE BEGIN HttpWebStatus := Response.StatusCode; ERROR(Response.StatusDescription); END; END; IF GETLASTERRORTEXT = 'OK' THEN BEGIN IF NOT ISNULL(ResponseStream) THEN BEGIN .....
Regards.
Please notice that an API can also return HTTP 201 for example. So only checking for OK is not (always) good enough.1 -
real horror story in hindi
THEN BEGIN-1 -
Hello @azharsaeedkhan
In NAV 2013 that don't have a try function I do something like thisASSERTERROR BEGIN Response := Request.GetResponse(); IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN ResponseStream := Response.GetResponseStream(); ERROR('OK'); END ELSE BEGIN HttpWebStatus := Response.StatusCode; ERROR(Response.StatusDescription); END; END; IF GETLASTERRORTEXT = 'OK' THEN BEGIN IF NOT ISNULL(ResponseStream) THEN BEGIN .....
Regards.
Thank you @ftornero this is very helpful0 -
irasoelbaks wrote: »azharsaeedkhan wrote: »> @irasoelbaks said:
> The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
>
> Pseudo code:
> IF NOT HttpWebRequest.GetResponse THEN ERROR('')
>
> I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
>
> Pseudo code:
> if HttpStatusCode = 200 THEN
> EXIT(True) ELSE // Success
> ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
It doesn't return a boolean, but HttpWebResponse object.
The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
@irasoelbaks i am looking at this function
Please let me know, which GetResponse function you are talking about.0 -
irasoelbaks wrote: »Hello @azharsaeedkhan
In NAV 2013 that don't have a try function I do something like thisASSERTERROR BEGIN Response := Request.GetResponse(); IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN ResponseStream := Response.GetResponseStream(); ERROR('OK'); END ELSE BEGIN HttpWebStatus := Response.StatusCode; ERROR(Response.StatusDescription); END; END; IF GETLASTERRORTEXT = 'OK' THEN BEGIN IF NOT ISNULL(ResponseStream) THEN BEGIN .....
Regards.
Please notice that an API can also return HTTP 201 for example. So only checking for OK is not (always) good enough.
Yes you are right, but we can handle that in code0 -
azharsaeedkhan wrote: »irasoelbaks wrote: »azharsaeedkhan wrote: »> @irasoelbaks said:
> The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
>
> Pseudo code:
> IF NOT HttpWebRequest.GetResponse THEN ERROR('')
>
> I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
>
> Pseudo code:
> if HttpStatusCode = 200 THEN
> EXIT(True) ELSE // Success
> ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
It doesn't return a boolean, but HttpWebResponse object.
The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
@irasoelbaks i am looking at this function
Please let me know, which GetResponse function you are talking about.
@irasoelbaks i think you are talking about GetResponse function in codeunit 1297 Http Web Request Mgt. but i am working on NAV 2013 and it doesn't have this codeunit and try functions.
Sorry my bad, i should have mentioned NAV version in my question0 -
azharsaeedkhan wrote: »azharsaeedkhan wrote: »irasoelbaks wrote: »azharsaeedkhan wrote: »> @irasoelbaks said:
> The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
>
> Pseudo code:
> IF NOT HttpWebRequest.GetResponse THEN ERROR('')
>
> I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
>
> Pseudo code:
> if HttpStatusCode = 200 THEN
> EXIT(True) ELSE // Success
> ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong
It doesn't return a boolean, but HttpWebResponse object.
The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
@irasoelbaks i am looking at this function
Please let me know, which GetResponse function you are talking about.
@irasoelbaks i think you are talking about GetResponse function in codeunit 1297 Http Web Request Mgt. but i am working on NAV 2013 and it doesn't have this codeunit and try functions.
Sorry my bad, i should have mentioned NAV version in my question
No problem. I meant Codeunit 1297 indeed.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions