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

Posted 阮威敏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 getUrlStr = String.format("http://%s:%s/s-program/area/list", hostName, 8080);
        get(getUrlStr);
    }

    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);
    }
}

 

以上是关于Java-使用HttpURLConnection发送GET,POST请求的主要内容,如果未能解决你的问题,请参考以下文章

post请求baseauth怎么添加java

使用 java.net.HttpURLConnection 时是不是可以转储 HTTP 标头?

Java_HttpURLConnection使用

Java 使用HttpURLConnection 设置头部 设置的Authorization不成功

java使用HttpURLConnection发送Post数据

java 使用原生HttpURLConnection发送post请求