java.net.HttpURLConnection
PUT、DELETE リクエストを (実際に) HTTP ベースの URLに送信できるかどうかを知りたいです。
GET、POST、TRACE、OPTIONS リクエストを送信する方法を説明する記事をたくさん読みましたが、PUT および DELETE リクエストを正常に実行するサンプル コードはまだ見つかりません。
java.net.HttpURLConnection
PUT、DELETE リクエストを (実際に) HTTP ベースの URLに送信できるかどうかを知りたいです。
GET、POST、TRACE、OPTIONS リクエストを送信する方法を説明する記事をたくさん読みましたが、PUT および DELETE リクエストを正常に実行するサンプル コードはまだ見つかりません。
これは私にとってどのように機能したかです:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
int responseCode = connection.getResponseCode();
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
}
@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);
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();
}
UrlConnection は扱いにくい API です。HttpClient ははるかに優れた API であり、このスタックオーバーフローの質問が完全に示しているように、特定のことを達成する方法を探すのに時間を費やす必要がありません。これは、いくつかの REST クライアントで jdk HttpUrlConnection を使用した後に書いています。さらに、スケーラビリティ機能 (スレッドプール、接続プールなど) に関しては、HttpClient が優れています。
delete および put リクエストには簡単な方法があり_method
ます。投稿リクエストに " " パラメータを追加し、その値にPUT
" " または " " を書き込むだけで簡単に実行できます。DELETE