在调用 Rest API thur java 代码时得到空白响应,但邮递员工作正常

Getting blank response on invoking Rest API thur java code but thur postman working fine

在 postman 中,我正在调用 RestAPI。我选择了基本身份验证并传递了用户名和密码。这是工作。我得到回应。但是当我 运行 它通过 Java 代码并使用 Base64 编码进行身份验证时,我得到空白响应。下面附上快照:

Calling RestAPI thru Postman

Java 执行相同操作的代码如下:

public static String doPOSTCall(String postUrl, String authorization, JSONObject jo) throws Exception {
        String output = "";
        CloseableHttpClient httpClientPO = (HttpClients.createDefault());
        HttpPost httpPostPO = new HttpPost(postUrl);

        String auth = "ts_impl" + ":" + "Oracle@123";

        byte[] encodedAuth = Base64.encodeBase64(
                  auth.getBytes(StandardCharsets.ISO_8859_1));
                String authHeader = "Basic " + new String(encodedAuth);
                httpPostPO.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
        
        
        //httpPostPO.setHeader("Authorization", authorization);
        httpPostPO.setHeader("content-type", "application/json");
        StringWriter out = new StringWriter();
        jo.writeJSONString(out);
        String reqJsonString = out.toString();

        System.out.println("json2 is " + reqJsonString);
        StringEntity entityPO = new StringEntity(reqJsonString);

        System.out.println("Request : " + httpPostPO);

        httpPostPO.setEntity(entityPO);

        HttpResponse responsePO = httpClientPO.execute(httpPostPO);
        HttpEntity entityFromResponse = responsePO.getEntity();

        if (entityFromResponse != null) {

            output = EntityUtils.toString(responsePO.getEntity());
            System.out.println(output);
        }

        return output;
    }

您可以像这样创建编码的 header:

String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());

使用简单的 REST 客户端,您可以尝试以下代码:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration; 


    public class RestClient {
 
        public static void main(String args[]){
             
            String url = "Give-Your-URL-here";
            String name = "username";
            String password = "password";
            String authorization = name + ":" + password;
            
            JSONObject jo; // Create your Object 
            // Do the necessary stuffs with the jo object, feeding data etc.


            String response = doPOSTCall(url, String authorization, JSONObject jo);        
            System.out.println("response: "+response);
        }
    
        public static String doPOSTCall(String postUrl, String authorization, JSONObject jo) throws Exception {
            
            // Authentication Part
            String authStringEnc = new BASE64Encoder().encode(authorization.getBytes());
            System.out.println("Base64 encoded auth string: " + authStringEnc);
            
            // JSON to String
            StringWriter out = new StringWriter();
            jo.writeJSONString(out);
            String reqJsonString = out.toString();    
            System.out.println("json2 is " + reqJsonString);
            
            //REST calling
            Client restClient = Client.create();
            WebResource webResource = restClient.resource(url);
            ClientResponse resp = webResource.accept("application/json")
                                             .header("Authorization", "Basic " + authStringEnc)
                                             .post(ClientResponse.class, reqJsonString);

            // Response Verification
            if(resp.getStatus() != 200){
                System.err.println("Unable to connect to the server");
            }
            
            String output = resp.getEntity(String.class);
            return output;
        }
}

更新:

Maven Dependencies :    


<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.19.4</version>
</dependency>

开始工作了。问题是我在代码中使用了 http link,而它应该是 https.