2016-06-23 10 views
0

HttpURLConnection DELETEメソッドを使用してurlパラメータを送信する方法はありますか。HttpURLConnection URLパラメータを使用したDELETE

私はコードの下に使用しています:

私は常に無効initiator_idとして応答を取得しています上記のコードを実行しています
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(); 
} 

。私は郵便配達とカールで同じことをテストしました。カールと郵便配達の両方で働いています。

+0

質問パラメータ(URLの一部)またはフォームパラメータ(リクエストの本文に埋め込まれている)として 'initiator_id'を設定しようとしていますか? – dnault

+0

クエリのパラメータ(URLの一部)として追加しようとしています。フォームパラメータとして追加しようとしているときに、「HTTPメソッドDELETEが出力をサポートしていません」というメッセージが表示されます。 –

+0

フォームパラメータをDataOutputStreamとして送信していましたwr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes( "initiator_id = 6724181421"); wr.flush(); wr.close();しかし、例外 "HTTPメソッドDELETEは出力をサポートしません" Java 1.8で修正された –

答えて

0

ありがとうございます@dnault。 Java 1.8に更新し、フォームパラメータとしてinitiator_idを送信することで問題が解決されました。

関連する問題