英文:
Getting blank response on invoking Rest API thur java code but thur postman working fine
问题
在Postman中,我正在调用一个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("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;
}
希望这有助于您解决问题。
英文:
In postman, I am calling a RestAPI. I have selected Basic Auth and passing username and password. It is working. I am getting response. But when I run it thru Java code and do authentication using Base64 encoding, I am getting blank response. Snapshots are attached below:
Java code to do the same is below:
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;
}
答案1
得分: 0
你可以像这样创建已编码的标头:
String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());
英文:
You can to create the encoded header like this:
String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());
答案2
得分: 0
以下是您要翻译的内容:
使用简单的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 = "在此处填入您的URL";
String name = "用户名";
String password = "密码";
String authorization = name + ":" + password;
JSONObject jo; // 创建您的对象
// 对jo对象进行必要的操作,提供数据等等
String response = doPOSTCall(url, authorization, jo);
System.out.println("响应: " + response);
}
public static String doPOSTCall(String postUrl, String authorization, JSONObject jo) throws Exception {
// 认证部分
String authStringEnc = new BASE64Encoder().encode(authorization.getBytes());
System.out.println("Base64编码的认证字符串: " + authStringEnc);
// JSON转为字符串
StringWriter out = new StringWriter();
jo.writeJSONString(out);
String reqJsonString = out.toString();
System.out.println("json2是 " + reqJsonString);
// 发起REST调用
Client restClient = Client.create();
WebResource webResource = restClient.resource(url);
ClientResponse resp = webResource.accept("application/json")
.header("Authorization", "Basic " + authStringEnc)
.post(ClientResponse.class, reqJsonString);
// 验证响应
if (resp.getStatus() != 200) {
System.err.println("无法连接到服务器");
}
String output = resp.getEntity(String.class);
return output;
}
}
更新:
Maven依赖:
<!-- 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>
英文:
Use a simple REST Client, You can try the below code :
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;
}
}
Update :
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>
答案3
得分: 0
开始工作。问题是我在代码中使用了 HTTP 链接,而实际上应该使用 HTTPS 链接。
英文:
Started working. The problem was that I was using http link in the code, where as it should have been https.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论