简体   繁体   English

如何在Resttemplate Spring中做

[英]How to do in Resttemplate Spring

curl -u "uname:password" -H "X-Requested-With: Curl" -X "POST" "https://qualysapi.qg2.apps.qualys.eu/qps/rest/3.0/search/was/wasscan/" > was_finding.txt

如何在Spring rest模板中做同样的事情,

curl -u "uname:password" -H "X-Requested-With: Curl" -X "POST" "https://qualysapi.qg2.apps.qualys.eu/qps/rest/3.0/search/was/wasscan/" > was_finding.txt

Well, I'm not sure why you have the same request two times. 好吧,我不确定为什么您两次有相同的请求。 Anyhow, you need to initiate a rest client, that you can reuse, and then use it is as a simple http client for any calls: 无论如何,您需要启动一个REST客户端,您可以重用它,然后将其用作任何调用的简单http客户端:

// Do once
HttpClient client = HttpClients.createDefault();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(factory));

// Do anytime
byte[] plainCredsBytes = "uname:password".getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);

ResponseEntity<String> response = restTemplate.postForEntity(
        "https://qualysapi.qg2.apps.qualys.eu/qps/rest/3.0/search/was/wasscan/",
        new HttpEntity(headers), String.class);

if (response.getStatusCode() == HttpStatus.OK) {
    Files.writeString(Paths.get("was_finding.txt"), response.getBody());
} else {
    // Handle status code, etc.
}

If you need to set up SSL then you need to create a SSLContextBuilder before you build your client, thats it. 如果您需要设置SSL,则需要在构建客户端之前创建一个SSLContextBuilder,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM