码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-01-03 14:58:46      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:void   priority   示例   read   java   obj   wired   eth   class   

接口项目地址: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请求

标签:void   priority   示例   read   java   obj   wired   eth   class   

原文地址:https://www.cnblogs.com/NguyenVm/p/10213849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!