java小白一枚,想要求助各位大佬,像这种情况怎么解决:
报错情况如图:
光是这个日志,看不出来怎么回事。
大佬我私信你源代码你能帮我看看么(╹▽╹)
????有请求体,怎么能是 GET
请求,换POST试试看。
package com.transosmos.webservice.hcm.test;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
/**
* @Author: wwj
* @Date: 2020/8/24 23:36
*/
public class RestTemplateTest {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplateBuilder()
.basicAuthentication("DN_INTEGRATION", "QWaszx2020!")
.build();
HttpHeaders httpHeaders = new HttpHeaders();
String auth = "xxxxxxx" + ":" + "xxxxxxxx";
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
httpHeaders.add("Authorization", authHeader);
String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
"\n" +
"xmlns:ucm=\"http://www.baidu.com/UCM\">\n" +
"<soapenv:Header/>\n" +
"\n" +
"<soapenv:Body>\n" +
"<ucm:GenericRequest webKey=\"cs\">\n" +
"<ucm:Service IdcService=\"GET_SEARCH_RESULTS\">\n" +
"\n" +
"<!--Optional:-->\n" +
"\n" +
"<ucm:Document>\n" +
"<!--Zero or more repetitions:-->\n" +
"<ucm:Field name=\"QueryText\"></ucm:Field>\n" +
"\n" +
"</ucm:Document>\n" +
"\n" +
"</ucm:Service>\n" +
"</ucm:GenericRequest>\n" +
"</soapenv:Body>\n" +
"</soapenv:Envelope>";
HttpEntity<String> formEntity = new HttpEntity<>(requestBody, httpHeaders);
httpHeaders.setContentType(MediaType.TEXT_XML);
ResponseEntity<String> res = restTemplate.exchange("https:xxxxxxxxxxx", HttpMethod.POST, formEntity, String.class);
System.out.println(res.getBody());
}
// public RestTemplate generateRestTemplate() {
// TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
//
// SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
// .loadTrustMaterial(null, acceptingTrustStrategy)
// .build();
//
// SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
//
// CloseableHttpClient httpClient = HttpClients.custom()
// .setSSLSocketFactory(csf)
// .build();
//
// HttpComponentsClientHttpRequestFactory requestFactory =
// new HttpComponentsClientHttpRequestFactory();
//
// requestFactory.setHttpClient(httpClient);
// RestTemplate restTemplate = new RestTemplate(requestFactory);
// return restTemplate;
// }
}
复制下去试试看
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
public class RestTemplateTest {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplateBuilder().basicAuthentication("*********", "*********")
.build();
String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + "\n"
+ "xmlns:ucm=\"http://www.oracle.com/UCM\">\n" + "<soapenv:Header/>\n" + "\n" + "<soapenv:Body>\n"
+ "<ucm:GenericRequest webKey=\"cs\">\n" + "<ucm:Service IdcService=\"GET_SEARCH_RESULTS\">\n" + "\n"
+ "<!--Optional:-->\n" + "\n" + "<ucm:Document>\n" + "<!--Zero or more repetitions:-->\n"
+ "<ucm:Field name=\"QueryText\"></ucm:Field>\n" + "\n" + "</ucm:Document>\n" + "\n"
+ "</ucm:Service>\n" + "</ucm:GenericRequest>\n" + "</soapenv:Body>\n" + "</soapenv:Envelope>";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_XML);
ResponseEntity<byte[]> res = restTemplate.exchange(
"https://**************", HttpMethod.POST,
new HttpEntity<>(requestBody, httpHeaders), byte[].class);
System.out.println(res);
}
}
没有 异常,获取到了服务器的响应 。但是,不知道是不是你要的结果。
这个问题解决了,谢谢大佬~