http delete 方法传参数遇到java.net.ProtocolException: DELETE does not support writing的问题...

最近在测试通过http delete method传参数调用服务端方法,遇到了java.net.ProtocolException: DELETE does not support writing

 

			try {
				url = new URL(path);
				HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
				httpCon.setDoOutput(true);
				httpCon.setRequestMethod(method);
				OutputStreamWriter out = new OutputStreamWriter(
						httpCon.getOutputStream());
				out.write(new Gson().toJson(params));
				out.close();			
				String urlStringsss = convertStreamToString(httpCon.getInputStream());
				InfoPrinter.printLog(convertStreamToString(httpCon.getInputStream()));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

  执行时捕捉到

java.net.ProtocolException: DELETE does not support writing

后来在万能的google帮助下,最终在stackoverflow上找到了答案,先来看国外的一位Coder怎么说

I do not believe that HTTP DELETE takes input - I believe it acts like a GET variant.

The implementation provided by HTTP Client seems to support this conjecture as well.

If you are looking to provide a delete with a body, you /might/ want to consider using a POST to a location that accepts a body.

But in answer to your question, no, delete does not accept a body. You can add query parameters but not a body.

HTTP DELETE 不支持通过output来write参数,只能像GET方法一样通过url paramter来传递参数,否则会报标题上遇到的错误。

转载于:https://www.cnblogs.com/fukai316/p/3862496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值