java – 以请求身份在获取请求中传递JSON数据

前端之家收集整理的这篇文章主要介绍了java – 以请求身份在获取请求中传递JSON数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我必须向url发送获取请求
http://onemoredemo.appspot.com/group?authToken=access_token&authMethod=oauth

请求体包含json对象,如下所示.

{"goupid":"some_variable"
}

以下是发送获取请求的一段java代码

URL url1=new URL("http://onemoredemo.appspot.com/group?authToken="+access_token+"&authMethod=oauth");
conn=(HttpURLConnection) url1.openConnection();
conn.addRequestProperty("Content-type","application/x-www-form-urlencoded");

conn.setRequestMethod("GET");
conn.setDoOutput(true);
JSONObject jj=new JSONObject();
HttpGet get;
get.

jj.put("groupid","testing@iritesh.com");
conn.addRequestProperty("Content-TYpe","application/json");
conn.getOutputStream().write(jj.toString().getBytes());
conn.connect();
InputStream is=conn.getInputStream();

我收到一个错误java.io.FileNotFoundException.

我从mozilla浏览器发送了一个请求到url
http://onemoredemo.appspot.com/group?authToken=ya29.AHES6ZRDl-RqiA8W0PhybU_hMluHrHRjlJBvq06Vze0izJq0Ovjc088&authMethod=oauth
它正在给我正确的答复,但现在它超过一个小时,所以acccesstoken到期.我知道它有奇怪的发送参数以及请求的请求,但我必须发送它.

请帮助如何在请求体中发送json对象以获取请求.

解决方法

不要这样做

读这个:
http://tech.groups.yahoo.com/group/rest-discuss/message/9962

“Yes. In other words,any HTTP request message is allowed to contain a
message body,and thus must parse messages with that in mind. Server
semantics for GET,however,are restricted such that a body,if any,
has no semantic meaning to the request. The requirements on parsing
are separate from the requirements on method semantics.

So,yes,you can send a body with GET,and no,it is never useful to
do so.

This is part of the layered design of HTTP/1.1 that will become clear
again once the spec is partitioned (work in progress).”

关于这个检查的其他有趣的讨论:

https://stackoverflow.com/a/978094/550967

https://stackoverflow.com/a/978173/550967

https://stackoverflow.com/a/978519/550967

原文链接:https://www.f2er.com/java/126148.html

猜你在找的Java相关文章