400 Request Error: The server was unable to understand the request due to a malformed syntax
General: This error should have many reasons, it is pointed out that because of the character set encoding cause 400, the main code: send the request to the server JSON parameter is Out.write (Json.getbytes ()) (read the operating system's character set, If the operating system differs from the server where the project is deployed), change to Out.writechars (JSON), or Out.write (json.getbytes (server code)). The following code is 16 lines
1 //Create a connection2URL url =NewURL (u);3HttpURLConnection connection =(HttpURLConnection) url.openconnection ();4Connection.setdooutput (true);5Connection.setdoinput (true);6Connection.setrequestmethod ("POST");7Connection.setrequestproperty ("Connection", "keep-alive");8Connection.setconnecttimeout (30000);9Connection.setreadtimeout (30000);TenConnection.setusecaches (false); OneConnection.setrequestproperty ("Content-type", "Application/json;charset=utf-8"); A Connection.connect (); - //POST Request -out =NewDataOutputStream (Connection.getoutputstream ()); theString JSON =message.tostring (); -Out.writechars (JSON);//This line is the key. I previously wrote Out.write (Json.getbytes ()); - System.out.println (JSON); - Out.flush (); + //Read Response -Reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); + String lines; AStringBuffer SB =NewStringBuffer (""); at while((lines = Reader.readline ())! =NULL) { -Lines =NewString (Lines.getbytes (), "Utf-8"); - sb.append (lines); - } -Jsonobject Jsstr =Jsonobject.fromobject (sb.tostring ()); - //Gets the response value, judging whether the validation passed inString code = (string) jsstr.get ("code"); -String msg= (String) jsstr.get ("MSG"); toSystem.out.println ("Code:" +code+ ", msg:" +msg); + //interface returns whether the validation data is passed - if("0". Equals (code)) { theresult = "Success"; *}Else{ $result = "Fail";Panax NotoginsengSYSTEM.OUT.PRINTLN ("Issued Error: Error reason for" + msg + "issued content:" +JSON); - } the reader.close (); + //Disconnect Connection A Connection.disconnect (); the
There have been some twists and turns, such as running on your own eclipse, not deploying to the server, the JSON data is the same, but this error occurs because of the different server code for your local server and deployment project.
In solving the problem, Baidu did a long time, because do not adapt to my project made some wrong changes, such as looking for space, in my JSON there is a space, but I use the post submission, and will not have an impact. also said to double quotation marks into single quotes, which makes the original local not error program also error, but I also silly happy to think it is the quotation marks. There is also the JSON encoding, but no matter what the JSON into what lattice encoding out.write (Json.getbytes ()) transmission when the encoding format is not set, or go to the default operating system encoding or not change the
About Java.io.IOException:Server returned HTTP response code:400 for URL error and string.getbytes () character Set