java http get / post 三方接口

//发送http请求给银行等三方接口,然后接收返回值
    public String sendStr(String ccbParam) throws IOException
    {
            URL url = new URL("https://xxx.xx.com.cn/CCBIS/xxx?id=123");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true); // 设置该连接是可以输出的
            connection.setRequestMethod("GET"); // 设置请求方式
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;"
                    + " charset=UTF-8");

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) { // 读取数据
                result.append(line + "\n");
            }
            connection.disconnect();      
            return result.toString();
    }

猜你喜欢

转载自www.cnblogs.com/tobetterlife/p/12173110.html
今日推荐