HttpURLConnection DELETE with url params

时间:2016-06-23 21:57:08

标签: java httpurlconnection http-delete

如何使用HttpURLConnection DELETE方法发送url params?

我正在使用以下代码:

String targetURL = "http://example.com/some:data?initiator_id=6724181421";
URL url = new URL(targetURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty ("dev_key", "12345");
connection.setReadTimeout(60000);
connection.setConnectTimeout(60000);
connection.setRequestMethod("DELETE");
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
}

当我执行上面的代码时,我总是将响应作为无效的initiator_id。我用邮差和卷曲测试了同样的东西。在curl和Postman都有效。

1 个答案:

答案 0 :(得分:0)

谢谢@dnault。更新到Java 1.8并将initiator_id作为表单参数发送修复了该问题。