java原生类java.net.HttpURLConnection的使用,实现get,post请求

 泛微oa对接企业微信api时在学习如何使用java发送网络请求.由于不会在泛微里加jar包,所以我放弃了okhttp3来发送网络请求,使用了java的原生类java.net来发送网络请求.

请求的代码原型我是用webapi中的代码示例中拉出来的,现在的代码是在其基础上修改的

参考链接:Http——HttpURLConnection详解 - 曹伟雄 - 博客园 (cnblogs.com)

 1.get请求

private static String doGet(String httpUrl) {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(6000);
            connection.connect();
            if (connection.getResponseCode() == 200) {
                inputStream = connection.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                StringBuilder sbf = new StringBuilder();
                String temp;
                while ((temp = bufferedReader.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append(System.getProperty("line.separator"));
                }
                result = sbf.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != connection) {
                connection.disconnect();
            }
        }
        return result;
    }

2.post请求

    public static String doPost(String httpUrl, String params) {
//        HttpURLConnection connection=null;
//        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
//        BufferedReader bufferedReader = null;
        String result= null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);
            // connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Type","application/json");
            // 需要转换编码格式,否则输出到企业微信时是乱码格式
            outputStreamWriter = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
            // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8"));
            // writer.write(params);
//            outputStream.write(params.getBytes());
            outputStreamWriter.write(params);
            outputStreamWriter.flush();
            if (connection.getResponseCode() == 200) {
                inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuffer sbf = new StringBuffer();
                String temp;
                while ((temp = bufferedReader.readLine()) !=null){
                    sbf.append(temp);
                    sbf.append(System.getProperty("line.separator"));
                }
                result=sbf.toString();
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                    outputStreamWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return result;
    }

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值