139

java.net.HttpURLConnectionPUT、DELETE リクエストを (実際に) HTTP ベースの URLに送信できるかどうかを知りたいです。

GET、POST、TRACE、OPTIONS リクエストを送信する方法を説明する記事をたくさん読みましたが、PUT および DELETE リクエストを正常に実行するサンプル コードはまだ見つかりません。

4

8 に答える 8

26

これは私にとってどのように機能したかです:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
int responseCode = connection.getResponseCode();
于 2011-02-16T19:23:31.277 に答える
13
public  HttpURLConnection getHttpConnection(String url, String type){
        URL uri = null;
        HttpURLConnection con = null;
        try{
            uri = new URL(url);
            con = (HttpURLConnection) uri.openConnection();
            con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setConnectTimeout(60000); //60 secs
            con.setReadTimeout(60000); //60 secs
            con.setRequestProperty("Accept-Encoding", "Your Encoding");
            con.setRequestProperty("Content-Type", "Your Encoding");
        }catch(Exception e){
            logger.info( "connection i/o failed" );
        }
        return con;
}

次に、コードで:

public void yourmethod(String url, String type, String reqbody){
    HttpURLConnection con = null;
    String result = null;
    try {
        con = conUtil.getHttpConnection( url , type);
    //you can add any request body here if you want to post
         if( reqbody != null){  
                con.setDoInput(true);
                con.setDoOutput(true);
                DataOutputStream out = new  DataOutputStream(con.getOutputStream());
                out.writeBytes(reqbody);
                out.flush();
                out.close();
            }
        con.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String temp = null;
        StringBuilder sb = new StringBuilder();
        while((temp = in.readLine()) != null){
            sb.append(temp).append(" ");
        }
        result = sb.toString();
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.error(e.getMessage());
    }
//result is the response you get from the remote side
}
于 2012-11-20T01:23:39.830 に答える
12

@adietisheim と HttpClient を提案する他の人々に同意します。

私は HttpURLConnection を使用して残りのサービスへの簡単な呼び出しを試みましたが、納得できず、その後 HttpClient を試してみましたが、本当に簡単で理解しやすく、優れていました。

put http 呼び出しを行うコードの例は次のとおりです。

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPut putRequest = new HttpPut(URI);

StringEntity input = new StringEntity(XML);
input.setContentType(CONTENT_TYPE);

putRequest.setEntity(input);
HttpResponse response = httpClient.execute(putRequest);
于 2012-05-17T13:52:32.333 に答える
4

HTML で PUT を正しく実行するには、try/catch で囲む必要があります。

try {
    url = new URL("http://www.example.com/resource");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setDoOutput(true);
    httpCon.setRequestMethod("PUT");
    OutputStreamWriter out = new OutputStreamWriter(
        httpCon.getOutputStream());
    out.write("Resource content");
    out.close();
    httpCon.getInputStream();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (ProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
于 2015-02-09T14:30:37.307 に答える
4

UrlConnection は扱いにくい API です。HttpClient ははるかに優れた API であり、このスタックオーバーフローの質問が完全に示しているように、特定のことを達成する方法を探すのに時間を費やす必要がありません。これは、いくつかの REST クライアントで jdk HttpUrlConnection を使用した後に書いています。さらに、スケーラビリティ機能 (スレッドプール、接続プールなど) に関しては、HttpClient が優れています。

于 2012-03-15T13:29:25.700 に答える
0

delete および put リクエストには簡単な方法があり_methodます。投稿リクエストに " " パラメータを追加し、その値にPUT" " または " " を書き込むだけで簡単に実行できます。DELETE

于 2018-09-17T09:25:54.650 に答える