如何在java中使用以下CURL命令?

时间:2016-04-22 09:24:01

标签: java android curl-commandline

这是网址

卷曲-H“授权:持票人oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L”https://api.url.com/v1/market/total-items.json

我想在JAVA上使用它,其中 oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L 值将是我使用变量接收的动态值。

1 个答案:

答案 0 :(得分:0)

这是您在Java中的操作方式。 curl-h由httpPost.addHeader()照顾 curl-d由表单主体 curl -u至少在此API(例如stripe)的情况下是授权载体。如果是Oauth2,curl -u在标题中传递username:password,例如

form.add(new BasicNameValuePair("username", request.getParameter("username")));

CloseableHttpClient userWebTokenHTTPclient;
        if (HIFIConstants.FULL_URL_HIFINITE.contains("localhost")) {  //for localhost HTTP request fails due to certificate issues, hence workaround. Works for Beta etc without SSLContext

            SSLContextBuilder sslcontext = new SSLContextBuilder();
            sslcontext.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            userWebTokenHTTPclient = HttpClients.custom().setSSLContext(sslcontext.build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .build();
        } else
            userWebTokenHTTPclient = HttpClients.createDefault();



        HttpPost httpPost = new HttpPost("https://api.somepublicsite.com/v1/ident/verits");

        List<NameValuePair> form = new ArrayList<>();
        form.add(new BasicNameValuePair("return_url", "https://example-dev.example.com?vi={VERIFICATION_INTENT_ID}"));
        form.add(new BasicNameValuePair("requested_verifications[0]", "identity_document"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
        httpPost.setEntity(entity);

        httpPost.addHeader("Authorization", "Bearer somesecretkeyhere"); 
        httpPost.addHeader("Version", "v3");
        httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");    
        CloseableHttpResponse tokenResponse = userWebTokenHTTPclient.execute(httpPost);
        org.json.JSONObject verificationIntent = new org.json.JSONObject(EntityUtils.toString(tokenResponse.getEntity()));
        userWebTokenHTTPclient.close();