0
Wie URL-Parameter mit HttpURLConnection DELETE-Methode senden?HttpURLConnection DELETE mit URL-Parametern
ich unten Code verwenden:
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();
}
Wenn ich den obigen Code bin Ausführung Ich bin immer wieder die Antwort als ungültig initiator_id bekommen. Ich habe das gleiche mit Postman und Curl getestet. In Curl und Postman funktioniert es.
Versuchen Sie, "Initiator_id" als Abfrageparameter (Teil der URL) oder als Formularparameter (eingebettet in den Hauptteil der Anfrage) zu setzen? – dnault
versucht, als Abfrage params (Teil der URL) hinzuzufügen. Wenn ich versuche, als Formularparameter hinzuzufügen, erhalte ich die Ausnahme "HTTP-Methode DELETE unterstützt keine Ausgabe" –
Ich habe den form-Parameter als DataOutputStream gesendet wr = new DataOutputStream (connection.getOutputStream()); wr.writeBytes ("initiator_id = 6724181421"); wr.flush(); wr.close(); Aber Ausnahme erhalten "HTTP-Methode DELETE unterstützt keine Ausgabe" –