您没有权限查看该内容.
您可以试试:How to send PUT, DELETE HTTP request in HttpURLConnection in J***A [如何发放,Java在HTTP请求删除HttpURLConnection] - 问题-字节技术
How to send PUT, DELETE HTTP request in HttpURLConnection in J***A
如何发放,Java在HTTP请求删除HttpURLConnection
问题 (Question)
I have Restful WebServices, and i send POST and GET HTTP request, how to send PUT and DELTE request HTTP in httpURLConection with J***A.
我已经平安WebServices,我发的帖子和HTTP请求,如何发送HTTP请求和三角洲在httpurlconection与Java。
最佳*** (Best Answer)
url = new URL("http://localhost:8080/putservice");
} catch (MalformedURLException exception) {
exception.printStackTrace();
HttpURLConnection httpURLConnection =
DataOutputStream dataOutputStream =
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();
if (dataOutputStream != null) {
dataOutputStream.flush();
dataOutputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
if (httpsURLConnection != null) {
httpsURLConnection.disconnect();
url = new URL("http://localhost:8080/deleteservice");
} catch (MalformedURLException exception) {
exception.printStackTrace();
HttpURLConnection httpURLConnection =
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();
把URL url =
url = new URL("http://localhost:8080/putservice");
} catch (MalformedURLException exception) {
exception.printStackTrace();
HttpURLConnection httpURLConnection =
DataOutputStream dataOutputStream =
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();
if (dataOutputStream != null) {
dataOutputStream.flush();
dataOutputStream.close();
} catch (IOException exception) {
exception.printStackTrace();
if (httpsURLConnection != null) {
httpsURLConnection.disconnect();
删除URL url =
url = new URL("http://localhost:8080/deleteservice");
} catch (MalformedURLException exception) {
exception.printStackTrace();
HttpURLConnection httpURLConnection =
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();
*** (Answer) 2
you can override
protected void doPut(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOE
Performs the HTTP PUT the default implementation reports an
HTTP BAD_REQUEST error. The PUT operation is analogous to sending a
file via FTP. Servlet writers who override this method must respect
any Content-* headers sent with the request. (These headers include
content-length, content-type, content-transfer-encoding,
content-encoding, content-base, content-language, content-location,
content-MD5, and content-range.) If the subclass cannot honor a
content header, then it must issue an error response (501) and discard
the request. For more information, see the HTTP 1.1 RFC.
This method does not need to be either "safe" or "idempotent".
Operations requested through PUT can have side effects for which the
user can be held accountable. Although not required, servlet writers
who override this method may wish to save a copy of the affected URI
in temporary storage.
protected void doDelete(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException
Performs the HTTP DELETE the default implementation reports
an HTTP BAD_REQUEST error. The DELETE operation allows a client to
request a URI to be removed from the server. This method does not need
to be either "safe" or "idempotent". Operations requested through
DELETE can have side-effects for which users may be held accountable.
Although not required, servlet writers who subclass this method may
wish to save a copy of the affected URI in temporary storage.
您可以重写protected void doPut(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOE
执行HTTP PUT操作;默认实现 bad_request HTTP错误报告。PUT操作是类似于通过FTP发送文件。Servlet的作家重写此方法,必须尊重任何内容*头随请求发送。(这些头文件包括内容长度,内容类型,内容传输编码,内容的编码,基于内容的,内容的语言,内容的位置, content-md5,和内容的范围。)如果子类不能荣誉头内容,那么它必须发出一个错误的反应(501)和丢弃请求。更多信息,见HTTP RFC 1.1。 该方法不需要“安全”或“幂等”。操作要求通过把有副作用,用户会举行。虽然不是必须的, servlet的作家谁重写此方法,可以在临时存储保存一份受影响的URI 。和 protected void doDelete(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException
执行HTTP删除操作;默认的执行情况的报告一个HTTP bad_request误差。删除操作允许客户端请求URI是从服务器中删除。该方法不需要是“安全的”或“幂等”。通过删除请求操作可以有副作用,用户可能会被追究责任。虽然不是必须的,servlet作家类此方法可以希望URI临时保存一份受影响。
*** (Answer) 3
If I use the DELETE request from @BartekM, it get this exception:
java.net.ProtocolException: DELETE does not support writing
To fix it, I just remove this instruction:
// httpURLConnection.setDoOutput(true);
如果我使用删除”bartekm请求,它把这个例外: java.net.ProtocolException: DELETE does not support writing
修理它,我只是把这个指令: // httpURLConnection.setDoOutput(true);
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:>更多相关文章
24小时热门资讯
24小时回复排行
乌ICP备号&& 版权所有,未经书面授权禁止使用
YY新闻网