gpt4 book ai didi

java - 如何将 token 添加到 HttpPost Json 消息

转载 作者:行者123 更新时间:2023-12-02 13:09:03 26 4
gpt4 key购买 nike

我有这个方法来发送 Json 消息:

public static void sendRequestPost(JSONObject json) throws IOException, JSONException {

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {

HttpPost request = new HttpPost(Config.urlJSON);
StringEntity params = new StringEntity(json.toString());
request.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding());
request.addHeader("content-type", "application/json");
request.setEntity(params);
Header[] headers = request.getAllHeaders();

String headerFull = "";

for (int i = 0; i < headers.length; i++) {
headerFull += headers[i] + " ";
}

Log.debug(headerFull);

int statusCode = httpClient.execute(request).getStatusLine().getStatusCode();

Log.debug("[STATUS:" + statusCode + "]");

} catch (Exception ex) {
Log.debug(ex.toString());
} finally {
httpClient.close();
}
}

我对这种方法没有任何问题,但现在我需要发送 token 而不是基本身份验证。

我通过curl命令尝试了这一行,没有出现问题:

curl -X POST -H 'Content-Type: application/json' -d '{"pm25": 35, "timestamp": 147805158}' https://url.com/?access-token={Yoq3UGQqDKP4D1L3Y6xIYp-Lb6fyvavpF3Lm-8cD}

我得到了正确的响应,但我无法让它在 java 上工作,我只是得到一个 401 代码作为返回,这是我在 java 中尝试过的:

public static void sendRequestPostRenam(JSONObject json) throws IOException, JSONException {

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {

HttpPost request = new HttpPost(Config.urlJSON);
StringEntity params = new StringEntity(json.toString());
request.addHeader("Authorization", "Token " + Config.renamToken + "");
request.setEntity(params);

int statusCode = httpClient.execute(request).getStatusLine().getStatusCode();

Log.debug("[STATUS:" + statusCode + "]");

} catch (Exception ex) {
Log.debug(ex.toString());
} finally {
httpClient.close();
}
}

我尝试过使用以下方法,但没有成功:

  • request.addHeader("授权", "Token token="+ Config.token);
  • request.addHeader("授权", "承载"+ Config.token);
  • request.addHeader("-Authorization", "Bearer "+ Config.token);

编辑:

我已将代码更改为此,但没有收到 401 代码。

public static void sendRequestPostRenam(JSONObject json) throws IOException, JSONException {

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {

URIBuilder builder = new URIBuilder(Config.urlJSON).addParameter("access-token", Config.renamToken);
HttpPost request = new HttpPost(builder.build());

StringEntity params = new StringEntity(json.toString());
request.setEntity(params);

CloseableHttpResponse response = httpClient.execute(request);
String content = EntityUtils.toString(response.getEntity());
Log.debug(content);
int statusCode = response.getStatusLine().getStatusCode();

Log.debug("[STATUS:" + statusCode + "]");

} catch (Exception ex) {
Log.debug(ex.toString());
} finally {
httpClient.close();
}
}

内容的打印内容如下:

{"status":"error","info":{"timestamp":["Timestamp no puede estar vacío."]},"timestamp":1495057833}

就像 json 参数没有分配给实体对象一样。

编辑:

这是我的 json 对象:

{"ruido_exterior":0,"co2_exterior":0,"humedad_interior":0,"ruido_interior":0,"temperatura_exterior":0,"co_interior":0,"co2_interior":0,"co_exterior":0,"temperatura_interior":0,"pm_25":8,"pm_10":10,"humedad_exterior":0,"timestamp":1494978084000}

最佳答案

可以看到,在 cURL 请求中, token 被添加到 URL 而不是 Authorization header,Java 中也应该如此。

  URIBuilder builder = new URIBuilder(config.urlJSON).addParameter("access-token", Config.renamToken);
HttpPost request = new HttpPost(builder.build());

关于java - 如何将 token 添加到 HttpPost Json 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44034228/

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