如何解决在调用Rest API和Java代码时获得空白响应,但邮递员工作正常
在邮递员中,我正在调用RestAPI。我选择了基本身份验证并传递用户名和密码。这是工作。我正在回应。但是,当我通过Java代码运行它并使用Base64编码进行身份验证时,我得到的响应是空白。快照如下:
要执行相同操作的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;
}
解决方法
您可以这样创建编码后的标头:
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,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链接,应该是https。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。