Java_HttpRequest方法

来源:互联网 发布:linux 调试动态库 编辑:程序博客网 时间:2024/06/07 09:58
/**     * http调用     *     * @param httpUrl :请求接口     * @param httpArg :参数     * @return 返回结果     */    public static String httpRequest(String httpUrl, String httpArg) {        BufferedReader reader = null;        String result = null;        StringBuffer sbf = new StringBuffer();        httpUrl = httpUrl + "?" + httpArg;        try {            URL url = new URL(httpUrl);            HttpURLConnection connection = (HttpURLConnection) url                    .openConnection();            connection.setConnectTimeout(3000);//超时时间,单位毫秒,3000是3秒            connection.setReadTimeout(3000);            connection.setRequestMethod("GET");            // 填入apikey到HTTP header(百度接口)            //connection.setRequestProperty("apikey", "11111");            connection.connect();            InputStream is = connection.getInputStream();            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));//UTF-8            String strRead = null;            while ((strRead = reader.readLine()) != null) {                sbf.append(strRead);                sbf.append("\r\n");            }            reader.close();            result = sbf.toString();        } catch (Exception e) {            e.printStackTrace();        }        return result;    }

原创粉丝点击