0
votes

I'm pretty new to making HTTP connections and working with API's in Java, so I'm not sure where the problem lies. When I send out a POST connection request in order to send a JSON formatted String of text to the other side, I get an error back along with a 400 response code. When I look up that code, it seems my connection isn't properly formatted. Code is below, along with the error message. Please help! Thanks!

public void sendToAPI(String urlPass, String param) throws IOException {

    URL url = new URL(urlPass);
    HttpURLConnection connectionOut = (HttpURLConnection) url.openConnection();
    connectionOut.setRequestMethod("POST");
    connectionOut.setConnectTimeout(5000);
    connectionOut.setReadTimeout(5000);
    connectionOut.setRequestProperty("Content-Type", "application/json");
    connectionOut.setRequestProperty("Content-Length", Integer.toString(param.length()));
    connectionOut.setDoOutput(true);
    connectionOut.setDoInput(true);
    connectionOut.connect();

    DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
    stream.writeUTF(param);
    stream.flush();
    stream.close();

    int responsecode = connectionOut.getResponseCode();
    if(responsecode != 200) {
        System.out.println("Response Code is " + responsecode);             
    }

    BufferedReader in = new BufferedReader(
            new InputStreamReader(connectionOut.getInputStream()));
    String output;
    StringBuffer response = new StringBuffer();

    while ((output = in.readLine()) != null) {
        response.append(output);
    }
    in.close();

    //printing result from response
    System.out.println(response.toString());

}

Response Code is 400 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL:XXX

2
what is your url and param?Morteza Jalambadani
url="vautointerview.azurewebsites.net/api" + datasetId.get("datasetId") + "/answer" param="answer=" + dealers.toString() It's not showing but the url is properly formatted with http:// in front, just a simple String. Same with the parameter, it's just a String, which I don't think would trigger a 400 response code.Randall Lashley
Does param include multibyte characters? content-length might be wrong.Affe

2 Answers

0
votes

So after playing around with the DataOutputStream, I replaced the below code:

DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
stream.writeUTF(param);

With another example I found online:

OutputStream os = connectionOut.getOutputStream();
os.write(param.getBytes());
os.flush();
os.close();

I'm not sure yet why, but this suddenly got the proper response code I was looking for, so the format it was sent in matched what they requested. Thanks for all responses.

0
votes

u can try this code:

InputStream inputStream;
if (responseCode == 200) {
    inputStream = con.getInputStream();
} else {
    inputStream = con.getErrorStream();
}

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String lines;
while ((lines = reader.readLine()) != null) {
    builder.append(lines);
    builder.append(System.getProperty("line.separator"));
}

String retStr = builder.toString().trim();
reader.close();

System.out.println("retStr: " + retStr);