簡體   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