java.net 后台发送GET/POST请求获取数据

GET请求:

public void testGetAnotherSystem(){
        String urlAddress = "http://localhost:8091/api/reservoir/getAllWarehouse";
        try {
            URL url = new URL(urlAddress);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.addRequestProperty("encoding","UTF-8");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("GET");
            //connection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
            //注意GET请求参数是直接写在请求URL里的,因此设置content-type也没用

            OutputStream outputStream = connection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String params = "warehouseBH=B"; //a=1&b=2
            bufferedWriter.write(params);
            bufferedWriter.flush();

            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line);
            }

            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

            String result = stringBuilder.toString();
            JSONObject jsonObject = (JSONObject) JSON.parse(result);
            JSONObject data = jsonObject.getJSONObject("data");
            Map<String,Object> map = JSONObject.toJavaObject(data,Map.class);
            for(Map.Entry<String,Object> entry:map.entrySet()){
                System.out.println(entry.getKey()+"-"+entry.getValue());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

POST请求:

public void testPostAnotherSystem(){
        String urlAddress = "http://localhost:8080/getData";
        try {
            URL url = new URL(urlAddress);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.addRequestProperty("encoding","UTF-8");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            //注意请求类型应与接口要求参数类型相对应
            //普通参数类型(@RequestParm String str)  application/x-www-form-urlencoded 
            //对象类型(@RequestBody Object obj)  application/json
            //上传文件(@RequestParam MultipartFile file) multipart/form-data
            
            
            OutputStream outputStream = connection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String content = "str="+ URLEncoder.encode("hahaha","UTF-8");
            bufferedWriter.write(content);
            bufferedWriter.flush();

            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

            String result = stringBuilder.toString();
            System.out.println(result);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

有关JSON数据解析用到 fastjson依赖包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值