gpt4 book ai didi

java - 获取 JSON 响应时获取 411 响应代码

转载 作者:行者123 更新时间:2023-11-30 11:31:25 28 4
gpt4 key购买 nike

在我的应用程序中,我尝试使用 POST 请求获取响应。响应服务器以 Json 格式发送给我。但是在添加属性之后,它返回给我的响应代码为 411(即内容长度问题)。我已经添加了内容长度。那么我没有得到的问题在哪里。这是我的代码:

String url = "https://xxx:8243/people/v3";
STRURL = url + HttpComm.getConnectionString().trim();

StringBuffer postData = new StringBuffer();
HttpConnection httpConnection = null;
try {
httpConnection = (HttpConnection) Connector.open(STRURL);
} catch (IOException e1) {
e1.printStackTrace();
};

try {
httpConnection.setRequestMethod("POST");
postData.append("?username="+user);
postData.append("&password="+password);
String encodedData = postData.toString();
byte[] postDataByte = postData.toString().getBytes("UTF-8");
httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
httpConnection.setRequestProperty("Content-Type","application/json");
httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
OutputStream out = httpConnection.openOutputStream();
DataOutputStream dos = new DataOutputStream(out);
out.write(postData.toString().getBytes());
out.flush();
int statusCode = httpConnection.getResponseCode();
Logger.out("HttpComm", "status code::::::: "+statusCode);
if (statusCode != HttpConnection.HTTP_OK)
{

}

更新代码:

HttpConnection httpConnection = null;
try {
httpConnection = (HttpConnection) Connector.open(STRURL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
try {
httpConnection.setRequestMethod("POST");
URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
postData.append("username", user);
postData.append("password", password);
byte[] postDataByte = postData.getBytes();
httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
OutputStream out = httpConnection.openOutputStream();
out.write(postDataByte);
out.flush();

int statusCode = httpConnection.getResponseCode();
Logger.out("HttpComm", "status code::::::: "+statusCode);

最佳答案

有些地方看起来不太对劲。我会推荐尝试这个:

 httpConnection.setRequestMethod("POST");

URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
postData.append("username", user);
postData.append("password", password);
byte[] postDataByte = postData.getBytes();

httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));

OutputStream out = httpConnection.openOutputStream();
DataOutputStream dos = new DataOutputStream(out);
out.write(postDataByte);
out.flush();

int statusCode = httpConnection.getResponseCode();
Logger.out("HttpComm", "status code::::::: "+statusCode);
if (statusCode != HttpConnection.HTTP_OK)

我改变了什么:

  1. 正如@samlewis 所说,代码正在创建一个变量来保存发布的数据字节,但在调用 out.write() 时并未使用它。

  2. 代码将内容类型设置为 JSON,但它并未发送 JSON。该请求只是两个参数。响应可能是 JSON,但您没有在请求的 Content-Type 参数中指定它。

  3. 用户名/密码参数仅使用字符串进行编码。通常,最好使用 URLEncodedPostDatato hold your POST parameters .

  4. 如果你打算使用字符串,我认为在用户名参数的前面添加一个 ? 仍然是不正确的。如果您想在 GET URL 中对参数进行编码,则可以使用 https://xxx:8243/people/v3?username=user&password=password。但是,这段代码使用的是 POST,而不是 GET。

  5. 还有一个未使用的 encodedData 变量。

关于java - 获取 JSON 响应时获取 411 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17175630/

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