带有 url 参数的 HttpURLConnection DELETE
HttpURLConnection DELETE with url params
如何使用 HttpURLConnection DELETE 方法发送 url 参数?
我正在使用以下代码:
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。我已经用 Postman 和 curl 进行了相同的测试。在 curl 和 Postman 中它都在工作。
谢谢@dnault。更新到 Java 1.8 并发送 initiator_id 作为表单参数解决了这个问题。
如何使用 HttpURLConnection DELETE 方法发送 url 参数?
我正在使用以下代码:
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。我已经用 Postman 和 curl 进行了相同的测试。在 curl 和 Postman 中它都在工作。
谢谢@dnault。更新到 Java 1.8 并发送 initiator_id 作为表单参数解决了这个问题。