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