HttpWebRequest returns Badrequest (400) and returns response

Source: Internet
Author: User

Today when using Fiddler to analyze a login feature of the Android app, the account and password errors return the corresponding message, and the status code is 400.

Normal usage:

1         /// <summary>2         ///read the returned content3         /// </summary>4         /// <param name= "asyncresult" ></param>5         Private voidreadcallback (IAsyncResult asyncresult)6         {7String result =String.Empty;8             Try9             {TenHttpWebRequest Myrequest =(HttpWebRequest) asyncresult. asyncstate; OneHttpWebResponse response =(HttpWebResponse) myrequest. EndGetResponse (asyncresult); A  -                 //read returned object -Stream Responsestream =Response. GetResponseStream (); the  -                 using(varRender =NewStreamReader (Responsestream, Encoding.UTF8)) -                 { -result =render. ReadToEnd (); + render. Close (); -                 } +             } A             Catch(Exception ex) at             { -  -Console.WriteLine ("An exception occurred:"+Ex. Message); -             } -             //Handling Return Messages -             if(Handler! =NULL) in Handler (result); -}

When execution GetResponseStream throws an exception, the exception hint is badrequest.

To get 400 of the information returned, you need to do special handling of the catch section.

To add a catch code:

1                 using(WebResponse response =e.response)2                 {3HttpWebResponse HttpResponse =(HttpWebResponse) response;4                     using(Stream data =Response. GetResponseStream ())5                     using(varReader =NewStreamReader (data))6                     {7                         stringText =Reader. ReadToEnd ();8 Console.WriteLine (text);9                     }Ten}

This will get the message returned to 400.

StackOverflow Questions Link:

Http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba

Http://stackoverflow.com/questions/11660947/httpwebrequest-getresponse-throws-bad-request-400-error

HttpWebRequest returns Badrequest (400) and returns response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.