100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java中delete请求 如何在JAVA中的HttpURLConnection中发送PUT DELETE HTTP请求

java中delete请求 如何在JAVA中的HttpURLConnection中发送PUT DELETE HTTP请求

时间:2023-03-18 18:14:18

相关推荐

java中delete请求 如何在JAVA中的HttpURLConnection中发送PUT DELETE HTTP请求

I have Restful WebServices, and i send POST and GET HTTP request, how to send PUT and DELTE request HTTP in httpURLConection with JAVA.

解决方案

PUT

URL url = null;

try {

url = new URL("http://localhost:8080/putservice");

} catch (MalformedURLException exception) {

exception.printStackTrace();

}

HttpURLConnection httpURLConnection = null;

DataOutputStream dataOutputStream = null;

try {

httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

httpURLConnection.setRequestMethod("PUT");

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());

dataOutputStream.write("hello");

} catch (IOException exception) {

exception.printStackTrace();

} finally {

if (dataOutputStream != null) {

try {

dataOutputStream.flush();

dataOutputStream.close();

} catch (IOException exception) {

exception.printStackTrace();

}

}

if (httpsURLConnection != null) {

httpsURLConnection.disconnect();

}

}

DELETE

URL url = null;

try {

url = new URL("http://localhost:8080/deleteservice");

} catch (MalformedURLException exception) {

exception.printStackTrace();

}

HttpURLConnection httpURLConnection = null;

try {

httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

httpURLConnection.setRequestMethod("DELETE");

System.out.println(httpURLConnection.getResponseCode());

} catch (IOException exception) {

exception.printStackTrace();

} finally {

if (httpURLConnection != null) {

httpURLConnection.disconnect();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。