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

如何在JAVA中的HttpURLConnection中发送PUT,DELETE HTTP请求,第1张

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

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-urlenpred");    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();    }}

删除

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-urlenpred");    httpURLConnection.setRequestMethod("DELETE");    System.out.println(httpURLConnection.getResponseCode());} catch (IOException exception) {    exception.printStackTrace();} finally {  if (httpURLConnection != null) {        httpURLConnection.disconnect();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5587187.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存