你out了,赶紧换
RestTemplate 吧!
进入正题,直接实战!!!
import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class TestUrl { @Autowired private RestTemplate restTemplate; /* * get无参 */ @Test public void testNoParameter() { String object = restTemplate.getForObject( "http://127.0.0.1/findAllStorageDevice", String.class); System.out.println("11111111111" + object); } /* * get有参 */ @Test public void testYesParameter() { Map<String, String> map = new HashMap(); map.put("sdId", "res$cc$20180524113123$962c4ded-d1df-49ca-92d3-cfbca5eb28ea"); String object = restTemplate.getForObject( "http://127.0.0.1/findStorageDeviceById?sdId={sdId}", String.class, map); System.out.println("11111111111" + object); } /* * post */ @Test public void testPost() { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("username", "11"); map.add("password", "22"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers); ResponseEntity<String> postForEntity = restTemplate.postForEntity("http://127.0.0.1/login", map, String.class); System.out.println("11111111111" + postForEntity); } }
package app.util; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; public class urlTest { public static void main(String[] args) throws InterruptedException { RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); //map.add("opUsername", "32322"); HttpHeaders headers = new HttpHeaders(); restTemplate.setErrorHandler(new CustomErrorHandler()); headers.add("token", "5a140050-ef25-4fe2-ada7-7ae98d6d2246"); HttpEntity<MultiValueMap<String, Object>> httpEntity1 = new HttpEntity<MultiValueMap<String, Object>>(map,headers); String url1="http://127.0.0.1/find"; ResponseEntity<String> entity1 = restTemplate.exchange(url1, HttpMethod.GET, httpEntity1, String.class); System.out.println("2"+entity1.getStatusCode()); System.out.println("2"+entity1.getBody()); } }