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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM