首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中使用以下CURL命令?

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

Stack Overflow用户
提问于 2016-04-22 09:24:01
回答 1查看 452关注 0票数 1

这是URL

curl -H“授权:无记名oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L”https://api.url.com/v1/market/total-items.json

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

EN

回答 1

Stack Overflow用户

发布于 2020-04-06 15:09:18

在Java中就是这样做的。curl -h由httpPost.addHeader() curl由表单curl -u负责,至少在这个API (例如条带)的情况下,它是授权行者。在Oauth2的情况下,curl -u在头文件中传递用户名:密码。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36789931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档