繁体   English   中英

JAVA - 发布 API 呼叫

[英]JAVA - POST API CALL

我正在尝试使用基本授权调用 API 但没有成功。

ClientConfig clientConfig = new ClientConfig();

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");
clientConfig.register( feature) ;

clientConfig.register(JacksonFeature.class);

Client client = ClientBuilder.newClient( clientConfig );
String URL = "http://localhost:8080/teste/" + taskId + "/start";
WebTarget webTarget = client.target(URL);

Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();

System.out.println(response.getStatus());
System.out.println(response.getStatusInfo());

if(response.getStatus() == 200)
{
    System.out.println(response.getStatus());
}

我收到很多错误,例如:

在 org.glassfish.jersey.message.internal.SourceProvider$SaxSourceReader class 中找不到合适的构造函数。 java.lang.IllegalArgumentException:在具体化 SystemDescriptor 时发现错误(

你能帮我吗? 一些技巧?

问候

字符串 URL = "http://localhost:8080/teste/" + taskId + "/enable"; HttpPut 请求 = 新 HttpPut(URL);

    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(
            AuthScope.ANY,
            new UsernamePasswordCredentials("user", "password")
    );

    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .build()) {
        try (CloseableHttpResponse response = httpClient.execute(request)) {

            // 401 if wrong user/password
            System.out.println(response.getStatusLine().getStatusCode());

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我现在正在使用此解决方案及其工作,但我需要在此请求中发送原始正文,我该怎么做?

任何提示?

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM