使用RestTemplate远程调用
package com.test.demo7000.resttemplate;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.time.Duration;
public class RestTemplateDemo4 {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofSeconds(60))
.setReadTimeout(Duration.ofSeconds(60))
.build();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
headers.add("module","***");
headers.add("action","get***");
JsonObject body= new JsonObject();
body.put("transaction_id", "***");
body.put("ebay_itemid", "***");
HttpEntity<String> entity = new HttpEntity<>(body.toString(), headers);
String url = "http://***";
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
System.out.println(result.getBody());
}
}