以httpPost Java形式的curl命令

时间:2015-10-11 04:25:00

标签: java curl http-post apache-commons-httpclient

有人可以帮我把它写成Java中的httpPost吗? 我只是无法做对

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"

提前致谢

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
URI uri = new URI("https://api.stormpath.com/v1/applications/7Jl082Y0Rsff2IBmtL8q2F/accounts");
httpPost.setURI(uri);
httpPost.addHeader(BasicScheme.authenticate(
        new UsernamePasswordCredentials(id, secret),
        HTTP.UTF_8, false));

httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type", "application/json");

httpPost.setEntity(new StringEntity(json[0].toString(), HTTP.UTF_8));

HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
        inputStream));

我不知道这是否有助于你。请有人帮我写这个curl命令。 =(ahah hthanks再次

0 个答案:

没有答案