Java-使用HttpURLConnection发送GET,POST请求

接口项目地址:https://github.com/Nguyen-Vm/s-program

API:

@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private Map<String, Object> listArea(){
        Map<String, Object> modelMap = new HashMap<>();
        List<Area> areaList = areaService.getAreaList();
        modelMap.put("list", areaList);
        return modelMap;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    private Map<String, Object> insertArea(@RequestBody Area area){
        Map<String, Object> modelMap = new HashMap<>();
        boolean result = areaService.addArea(area);
        modelMap.put("result", result);
        return modelMap;
    }

}

发送GET请求代码示例:

public class Main {

    public static void main(String[] args) throws IOException {

        InetAddress inetAddress = InetAddress.getLocalHost();
        // 获取本地IP
        String hostName = inetAddress.getHostAddress();

        String getUrl = String.format("http://%s:%s/s-program/area/list", hostName, 8080);
        get(getUrl);
    }

    public static void get(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb);
    }

}

发送POST请求代码示例:

public class Main {

    public static void main(String[] args) throws IOException {

        InetAddress inetAddress = InetAddress.getLocalHost();
        // 获取本地IP
        String hostName = inetAddress.getHostAddress();

        String postUrlStr = String.format("http://%s:%s/s-program/area/insert", hostName, 8080);
        post(postUrlStr, "{\"areaName\": \"中国上海\", \"priority\": 1}");


    }

    public static void post(String urlStr, String body) throws IOException {
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置Content-Type
        connection.setRequestProperty("Content-Type", "application/json");
        // 设置是否向httpUrlConnection输出,post请求设置为true,默认是false
        connection.setDoOutput(true);

        // 设置RequestBody
        PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
        printWriter.write(body);
        printWriter.flush();

        // 返回结果-输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb);
    }
}

猜你喜欢

转载自www.cnblogs.com/NguyenVm/p/10213849.html