java中无主体调用Post请求

java中无主体调用Post请求,java,api,rest,web-services,httpurlconnection,Java,Api,Rest,Web Services,Httpurlconnection,我有一个post API,它不接受任何输入。我必须从API获取输出。但它给出了编译错误 HttpURLConnection connection = null; String targetUrl="https://idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1/bots/"+BotID+"/dynamicEntities/"+

我有一个post API,它不接受任何输入。我必须从API获取输出。但它给出了编译错误

HttpURLConnection connection = null;
String targetUrl="https://idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1/bots/"+BotID+"/dynamicEntities/"+dynamicEntityId+"/pushRequests
URL url = new URL(targetUrl);
connection=(HttpURLConnection) url.openConnection();
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization", "Basic aWRjcy1vZGEtOTQxN2Y5MzU2MGI5NGViOGEyZTJhNGM5YWFjOWEzZmYtdDBfQVBQSUQ6MjQ0YWU4ZTItNmY3MS00YWYyLWI1Y2MtOTExMDg5MGQxNDU2");
connection.setRequestProperty("Accept", "application/json");
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");    
**osw.write();**   //this line is expecting input in parameter
osw.flush();
osw.close();
os.close(); 
connection.connect();

如果我在
osw.write()
中没有传递任何值,就会产生编译错误。如何解决相同的问题。

查看以下post调用的方法。您需要将outputstream添加到
osw.write()
,因为它需要一个参数

private static void sendPOST() throws IOException {
    URL obj = new URL(POST_URL);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);

    // For POST only - START
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();
    // For POST only - END

    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

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

        // print result
        System.out.println(response.toString());
        } 
    else {
        System.out.println("POST request not worked");
    }
}

有关上述代码的更多详细信息,请查看以下post调用方法。您需要将outputstream添加到
osw.write()
,因为它需要一个参数

private static void sendPOST() throws IOException {
    URL obj = new URL(POST_URL);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);

    // For POST only - START
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();
    // For POST only - END

    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

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

        // print result
        System.out.println(response.toString());
        } 
    else {
        System.out.println("POST request not worked");
    }
}

有关上述代码的更多详细信息,请查看。

谢谢@Pritam。这可能对我有用。这里的POST_参数是什么。这是一个编译错误。它是常数吗?我没有此API的任何参数。因为您没有任何post参数,所以可以删除与
post\u PARAM
相关的任何内容。对于
write()
方法,请看这里:谢谢Pritam。write()接受参数,因此我声明了一个字符串。字符串数据=”;然后将数据传递到write(数据)。在没有传递任何信息的情况下,它给出了编译错误。谢谢@Pritam。这可能对我有用。这里的POST_参数是什么。这是一个编译错误。它是常数吗?我没有此API的任何参数。因为您没有任何post参数,所以可以删除与
post\u PARAM
相关的任何内容。对于
write()
方法,请看这里:谢谢Pritam。write()接受参数,因此我声明了一个字符串。字符串数据=”;然后将数据传递到write(数据)。在没有传递任何信息的情况下,它给出了编译错误。