java 在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26923658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:53:03  来源:igfitidea点击:

Send PUT, DELETE HTTP request in HttpURLConnection

javaweb-serviceshttp-posthttpurlconnectionhttp-delete

提问by manitaz

I have created web service call using java below code. Now I need to make delete and put operations to be perform.

我使用下面的代码创建了 Web 服务调用。现在我需要执行删除和放置操作。

URL url = new URL("http://example.com/questions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod( "POST" );
conn.setRequestProperty("Content-Type", "application/json");

OutputStream os = conn.getOutputStream();
os.write(jsonBody.getBytes());
os.flush();

When I add below code to perform DELETE action it gives errors saying:

当我添加以下代码以执行 DELETE 操作时,它会给出错误消息:

java.net.ProtocolException: HTTP method DELETE doesn't support output.

java.net.ProtocolException: HTTP 方法 DELETE 不支持输出。

conn.setRequestMethod( "DELETE" );

So how to perform delete and put requests?

那么如何执行删除和放置请求呢?

回答by Jamsheer

PUTexample using HttpURLConnection:

PUT使用示例HttpURLConnection

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

DELETEexample using HttpURLConnection:

DELETE使用示例HttpURLConnection

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

回答by Young Emil

FOR PUT

放置

URL 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();

FOR DELETE

删除

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();

Actually got it from the link here: Send PUT, DELETE HTTP request in HttpURLConnection

实际上是从这里的链接得到的: Send PUT, DELETE HTTP request in HttpURLConnection

回答by Jamsheer

i suggest you to use restlet client for web service request .please refer the bellow sample code ,it may help you

我建议您使用 restlet 客户端进行 Web 服务请求。请参考下面的示例代码,它可能对您有所帮助

 Client client = new Client(new Context(), Protocol.HTTP);
   clientResource = new ClientResource(url);
        ResponseRepresentation responseRep = null;
         try {
            clientResource.setNext(client);           
            clientResource.delete();

       } catch (Exception e) {
           e.printStackTrace();

       }