简体   繁体   English

如何将令牌添加到HttpPost Json消息

[英]How can I add a token to a HttpPost Json message

I have this method to send a Json message: 我有这种方法来发送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();
    }
}

I have no problems with this method but now I need to send a token instead the basic authentification. 我使用此方法没有问题,但是现在我需要发送令牌代替基本身份验证。

I tried this line by a curl command without problems: 我通过curl命令尝试了这一行,没有任何问题:

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

And I get a correct response but I couldn't make it work on java I'm just getting a 401 code in return, this is what I have tried in java: 而且我得到了正确的响应,但是我无法使其在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();
        }
    }

I have tried using the following but without luck: 我尝试使用以下方法,但没有运气:

  • request.addHeader("Authorization", "Token token=" + Config.token); request.addHeader(“ Authorization”,“令牌令牌=” + Config.token);
  • request.addHeader("Authorization", "Bearer " + Config.token); request.addHeader(“ Authorization”,“ Bearer” + Config.token);
  • request.addHeader("-Authorization", "Bearer " + Config.token); request.addHeader(“-Authorization”,“ Bearer” + Config.token);

EDIT: 编辑:

I haved change the code to this and I'm not getting the 401 code. 我已经将代码更改为此,但没有得到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();
    }
}

The print of the content says: 内容打印显示:

{"status":"error","info":{"timestamp":["Timestamp no puede estar vacío."]},"timestamp":1495057833} {“状态”:“错误”,“信息”:{“时间戳”:[“时间戳无星空。”]},“时间戳”:1495057833}

It's like the json parameters it's not been asigned to the entity object. 就像json参数一样,它尚未分配给实体对象。

EDIT: 编辑:

This is my json object: 这是我的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} {“ ruido_exterior”:0,“ co2_exterior”:0,“ humedad_interior”:0,“ ruido_interior”:0,“ temperatura_exterior”:0,“ co_interior”:0,“ co2_interior”:0,“ co_exterior”:0,“ Tempaturata_interior“:0,” pm_25“:8,” pm_10“:10,” humedad_exterior“:0,” timestamp“:1494978084000}

Well seeing that in the cURL request, the token is added to URL and not the Authorization header, the same should be in Java. 很好地看到,在cURL请求中,令牌被添加到URL中,而不是Authorization标头中,这与Java中的情况相同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM