locked
HTTP web requests without execptions

    Question

  • Hi all,

    I want to get the data from server. and I don't want to thrown any execption by system. I have tried HttpClinet, and HttpWebRequest. For these two I am getting execptions from system like 404, 400, 500, 505, etc... But I want just the errorcode from system. I don't want any exception. these exceptions are decreasing my app performance. Please help me. I want to build app with optimum performance.

    Please suggest me.

    Thanks.


    Pallam Madhukar Windows Phone Developer

    Thursday, November 13, 2014 8:25 AM

Answers

  • I think you're in for a bit of trouble if you don't want exceptions in your application.  Exceptions are a perfectly normal part of c# and are thrown by nearly every part of the underlying c# APIs.

    You'll just have to handle exceptions.

    Exceptions in the case of a HTTP server error will be immensely faster than the time it takes to fail a DNS lookup or fail a connection to a server that doesn't exist.  I wouldn't worry about performance there.

    However, in order to minimize exceptions, use HttpClient.  As long as it connects to the server, you can use the HttpResponseMessage object it returns to check the status code without an exception.  Just don't call EnsureSuccessStatusCode.


    Darin R.

    Friday, November 14, 2014 5:42 PM
  • you can handle your exceptions server side and then determine what response you want to pass back with a try, catch block e.g:

    try{

    //do some work and catch an expectation you know might occur that you want to handle.

    } catch (System.NullReferenceException ex){

    //We know we have a null reference exception, maybe the user forgot to POST some data.

    //Lets return the status code here...

    } finally {

    //not required, but maybe there is some clean up to do here in code.

    }

    Friday, November 14, 2014 5:53 PM
  • It just doesn't make sense to try to prevent exceptions from being thrown when they actually occur. You should listen for the exceptions and then decide what you want to do with them, as the above post says.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, November 19, 2014 3:47 PM
    Moderator

All replies

  • Those are http status codes sent from the server.  I don't know how you can possibly avoid them, except to not make bad requests.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, November 13, 2014 7:02 PM
    Moderator
  • Hi Matt,

    The server http codes I need, But I don't want exceptions in my application. I manually check http codes and updates my UI. 

    As of my knowledge, Internal Windows system API throwing an exception for error codes. But I need somthing like: "which gives only status code. and if status code success, then reading response".

    Thanks


    Pallam Madhukar Windows Phone Developer


    Friday, November 14, 2014 5:35 AM
  • I think you're in for a bit of trouble if you don't want exceptions in your application.  Exceptions are a perfectly normal part of c# and are thrown by nearly every part of the underlying c# APIs.

    You'll just have to handle exceptions.

    Exceptions in the case of a HTTP server error will be immensely faster than the time it takes to fail a DNS lookup or fail a connection to a server that doesn't exist.  I wouldn't worry about performance there.

    However, in order to minimize exceptions, use HttpClient.  As long as it connects to the server, you can use the HttpResponseMessage object it returns to check the status code without an exception.  Just don't call EnsureSuccessStatusCode.


    Darin R.

    Friday, November 14, 2014 5:42 PM
  • you can handle your exceptions server side and then determine what response you want to pass back with a try, catch block e.g:

    try{

    //do some work and catch an expectation you know might occur that you want to handle.

    } catch (System.NullReferenceException ex){

    //We know we have a null reference exception, maybe the user forgot to POST some data.

    //Lets return the status code here...

    } finally {

    //not required, but maybe there is some clean up to do here in code.

    }

    Friday, November 14, 2014 5:53 PM
  • It just doesn't make sense to try to prevent exceptions from being thrown when they actually occur. You should listen for the exceptions and then decide what you want to do with them, as the above post says.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, November 19, 2014 3:47 PM
    Moderator