如何将curl调用转换为java HttpPostcall

时间:2014-06-23 12:15:47

标签: java curl http-headers

我有curl命令:

curl -k -f  --header "Authorization: bearer cb084803-xxxx-xxxx-xxxx-2a275e112c38 " 
--header "Content-Type:multipart/form-data" 
--header "X-PartnerUserId:login01" 
--form "title=fileName" 
--form "tag=mytag" 
--form "archive=@test.pdf;type=application/pdf" 
-X POST https://api.EXAMPLEAPI.com/api/v2.0/doc

我想在Java API中创建HTTP请求,它将执行相同的操作 但我不知道怎样才能为参数做什么(-form ....)

    Map<String, String> mapResponse = new HashMap<String, String>();
    String resourceURL = config.getProperty(OAuthConstants.RESOURCE_SERVER_URL_UPLOAD);
    OAuth2Details oauthDetails = createOAuthDetails(config);

    HttpPost httpPost = new HttpPost(resourceURL);
    httpPost.setHeader(OAuthConstants.AUTHORIZATION, getAuthorizationHeaderForAccessToken(oauthDetails.getAccessToken()));
    httpPost.setHeader(OAuthConstants.CONTENTE_TYPE, OAuthConstants.ENCODED_CONTENT_DATA);
    httpPost.setHeader(OAuthConstants.PARTNERUSERID, login);
    //------------------------------------------------------------------------
    httpPost.set...  // add: --form "title=fileName" 
    httpPost.set...  // add: --form "tag=mytag" 
    httpPost.set...  // add: --form "archive=@test.pdf;type=application/pdf" 
    //------------------------------------------------------------------------
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = null;
    int code = -1;
    try {
        response = httpClient.execute(httpPost);
        code = response.getStatusLine().getStatusCode();

任何帮助表示感谢。

0 个答案:

没有答案