Http请求工具类

    public class HttpUtils {
        /**
         * Get方式
         */
        public static String doGet(String url) {
            String result = "";
    //        try {
    //            // 当出现乱码时,可能是由于编码错误引起,尝试改变编码格式,使用URLEncoder.encode()方法
    //            url = url + "?username=" + URLEncoder.encode(name, "UTF-8") + "&pwd=" + URLEncoder.encode(password, "UTF-8");
    //        } catch (UnsupportedEncodingException e1) {
    //            e1.printStackTrace();
    //        }
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader bufferedReader = null;
            try {
                URL httpUrl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
                // 设置读取超时
                connection.setReadTimeout(5000);
                // 设置链接超时时间
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                // 获得链接请求码
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    is = connection.getInputStream();
                    isr = new InputStreamReader(is);
                    bufferedReader = new BufferedReader(isr);
                    String str = null;
                    StringBuffer sb = new StringBuffer();
                    while ((str = bufferedReader.readLine()) != null) {
                        sb.append(str);
                    }
                    System.out.println("Get方式的result:" + sb.toString());
                    result = sb.toString();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null)
                        is.close();
                    if (isr != null)
                        isr.close();
                    if (bufferedReader != null)
                        bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

        /**
         * Post方式
         */
        public static String doPost(String url, String content) {
            String result = "";
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader bufferedReader = null;
            try {
                // 获得当前系统信息
    //          Properties properties = System.getProperties();
    //          properties.list(System.out);

                URL httpUrl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(7000);
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                // Content-Length: 请求内容的长度
                connection.setRequestProperty("Content-Length", content.getBytes().length + "");
    //            Log.e("doPost", "content.length(): " + content.getBytes().length );

                // Cache-Control: 控制HTTP缓存的方法, 详情可见:
                // http://www.cnblogs.com/yuyii/archive/2008/10/16/1312238.html
    //            connection.setRequestProperty("Cache-Control", "max-age=0");
                // Origin: 请求发起者
    //            connection.setRequestProperty("Origin", "http://192.168.2.124:8080");
                OutputStream outputStream = connection.getOutputStream();
                // 提交数据
                outputStream.write(content.getBytes());
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    is = connection.getInputStream();
                    isr = new InputStreamReader(is);
                    bufferedReader = new BufferedReader(isr);
                    String str = null;
                    StringBuffer sb = new StringBuffer();
                    while ((str = bufferedReader.readLine()) != null) {
                        sb.append(str);
                    }
                    result = sb.toString();
                    System.out.println("Post方式的result:" + sb.toString());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                close(is);
                close(isr);
                close(bufferedReader);
            }
            return result;
        }


        public static String doJsonPost(String url, String content) {
            String result = "";
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader bufferedReader = null;
            try {
                URL httpUrl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(7000);
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                // Content-Length: 请求内容的长度
                connection.setRequestProperty("Content-Length", content.getBytes().length + "");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("Token", "Ivj6eZRx40+MTx2ZvnG8nA");
    //            Log.e("doPost", "content.length(): " + content.getBytes().length );

                // Cache-Control: 控制HTTP缓存的方法, 详情可见:
                // http://www.cnblogs.com/yuyii/archive/2008/10/16/1312238.html
    //            connection.setRequestProperty("Cache-Control", "max-age=0");
                // Origin: 请求发起者
    //            connection.setRequestProperty("Origin", "http://192.168.2.124:8080");
                OutputStream outputStream = connection.getOutputStream();
                // 提交数据
                outputStream.write(content.getBytes());
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    is = connection.getInputStream();
                    isr = new InputStreamReader(is);
                    bufferedReader = new BufferedReader(isr);
                    String str = null;
                    StringBuffer sb = new StringBuffer();
                    while ((str = bufferedReader.readLine()) != null) {
                        sb.append(str);
                    }
                    result = sb.toString();
                    System.out.println("Post方式的result:" + sb.toString());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                close(is);
                close(isr);
                close(bufferedReader);
            }
            return result;
        }


        /**
         * 关闭流
         */
        public static boolean close(Closeable io) {
            if (io != null) {
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值