-1

有人可以帮我用java中的httpRequest的形式写这个吗?我已经尝试了很多次,但都失败了。我不知道为什么,但我就是做错了=(

curl -X POST --user $YOUR_API_KEY_ID:$YOUR_API_KEY_SECRET \
 -H "Accept: application/json" \
 -H "Content-Type: application/json;charset=UTF-8" \
 -d '{
       "favoriteColor": "red",
       "hobby": "Kendo"
     }' \
"https://api.stormpath.com/v1/accounts/cJoiwcorTTmkDDBsf02bAb/customData"

(自定义数据必须是 json 格式)

任何帮助将不胜感激。=)

4

1 回答 1

3

    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpPost postRequest = new HttpPost(
            "https://api.stormpath.com/v1/accounts/cJoiwcorTTmkDDBsf02bAb/customData");

    String credentials = apiKey.getId() + ":" + apiKey.getSecret();
    postRequest.setHeader("Authorization", "Basic " + Base64.encodeBase64String(credentials.getBytes("UTF-8")));
    postRequest.setHeader("Accept", "application/json");

    StringEntity input = new StringEntity("{\"favoriteColor\":\"red\",\"hobby\":\"Kendo\"}");
    input.setContentType(ContentType.APPLICATION_JSON.toString());
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);
    System.out.println(response);
于 2015-10-26T20:51:40.927 回答