मेरे अनुरोध के जवाब में 400 त्रुटि आती है, वहां पोस्टमैन में चेक किया गया ठीक है। मुझे बताएं कि कोड में क्या गलत है?

मेरा कोड

public void doPost() throws SQLException{
    try {
        URL url = new URL("http://sbt-ot-289.ca.sbrf.ru:8080/ptd_integration/check_clients_stub");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();
        System.out.println("Connection succesfull");
        Scanner scanner = new Scanner(urlConnection.getInputStream());
        scanner.useDelimiter("\\Z");
        System.out.println(scanner.next());
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        urlConnection.disconnect();
    }
}

स्टैक ट्रेस

Connection succesfull
java.io.IOException: Server returned HTTP response code: 400 for URL: http://sbt-ot-289.ca.sbrf.ru:8080/ptd_integration/check_clients_stub
                at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
                at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
                at com.company.RequestSender.doPost(RequestSender.java:34)
                at com.company.Main.main(Main.java:10)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

पोस्टमैन में परिणाम

enter image description here

0
tomson888 3 पद 2018, 17:34

1 उत्तर

सबसे बढ़िया उत्तर

समस्या का समाधान हुआ

public void doPost() throws SQLException{
try {
    URL url = new URL("http://sbt-ot-289.ca.sbrf.ru:8080/ptd_integration/");
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestProperty("Content-Type", "application/json");
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();
    System.out.println("Connection succesfull");
    OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
    wr.write(usersCollection().toString());
    wr.flush();

    StringBuilder sb = new StringBuilder();
    int HttpResult = urlConnection.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK){
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
        String line;
       while ((line = br.readLine()) !=null){
            sb.append(line);
        }
        br.close();
        System.out.println(sb.toString());
    }else {
        System.out.println(urlConnection.getResponseMessage());
    }
} catch (IOException e) {
    e.printStackTrace();
}
finally {
    urlConnection.disconnect();
}

}

0
tomson888 4 पद 2018, 07:44