Java Equivalent of curl query

邮差的信 提交于 2019-12-25 02:28:42

问题


I am trying to fetch current user info after making him log into his box, using the box sdk for android. In the box api documentation, they have mentioned everything using curls. I am not familiar with curl. So, can anyone please give me a java equivalent for this curl operation :

curl https://api.box.com/2.0/users/me-H "Authorization: Bearer ACCESS_TOKEN". 

I have the users access token.So, please give me a java equivalent for the above curl operation.


回答1:


You can use java HttpClient

HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);

httpPost.setHeader("Authorization", "Bearer ACCESS_TOKEN"); // add headers if needded

//set params
BasicNameValuePair[] params = new BasicNameValuePair[] {new BasicNameValuePair("param1","param value"),
            new BasicNameValuePair("param2","param value")};

UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( Arrays.asList(params), "utf-8");
    httpPost.setEntity(urlEncodedFormEntity);

//execute request
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();

String body = EntityUtils.toString(entity); //here response in string you can parse it



回答2:


The HttpClient object that the other answer proposes is now deprecated. I solved a CURL problem like yours (with the extra difficulty of uploading a .wav file). Check out my code in this this answer/question. How to upload a WAV file using URLConnection



来源:https://stackoverflow.com/questions/22388323/java-equivalent-of-curl-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!