接口数据测试

import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class JTEST {
    /**
     * 接口测试:--地图坐标服务
     */
    @Test
    public void test1(){
        RestTemplate restTemplate = new RestTemplate();
        System.out.println("sss");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("key", "###############");
        map.add("lat", "39.908700982285396");
        map.add("lng", "116.3974965092");
        map.add("type", "6");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( "http://v.juhe.cn/offset/index", request , String.class );
        System.out.println(response.getBody());
        String status= response.getStatusCode().toString();
        System.out.println("status==>"+status);
        String result=response.toString();
        if(status.equals("200")){
            System.out.println("result==>地图坐标服务接口连接成功");
            System.out.println("result==>"+result);
        }
        System.out.println("result==>"+result);

    }
    @Test
    public void test2(){
        RestTemplate restTemplate = new RestTemplate();
        System.out.println("sss");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("key", "###############");
        map.add("page", "2");
        map.add("pagesize", "10");
        map.add("sort", "asc");
        map.add("time", "1418745237");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( "http://v.juhe.cn/joke/content/list.php", request , String.class );
        System.out.println(response.getBody());
        String status= response.getStatusCode().toString();
        System.out.println("status==>"+status);
        String result=response.toString();
        if(status.equals("200")){
            System.out.println("result==>笑话大全接口连接成功");
            System.out.println("result==>"+result);
        }
        System.out.println("result==>"+result);

    }
}

猜你喜欢

转载自blog.csdn.net/kevin_cat/article/details/83061110