gpt4 book ai didi

java - 在 get 请求中将 JSON 数据作为请求体传递

转载 作者:搜寻专家 更新时间:2023-10-30 21:47:05 24 4
gpt4 key购买 nike

你好,我必须向 url 发送获取请求
http://onemoredemo.appspot.com/group?authToken=access_token&authMethod=oauth

请求正文包含 json 对象,如下所示。

{"goupid":"some_variable"
}

下面是一段发送get请求的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
它给了我正确的回应,但现在已经超过一个小时,所以访问 token 过期了。我知道在 get 请求中发送参数和 requestbody 很奇怪,但我必须发送它。

请帮助如何在获取请求的请求正文中发送一个 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

关于java - 在 get 请求中将 JSON 数据作为请求体传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11575947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com