zoukankan      html  css  js  c++  java
  • http基本get和post请求

    get请求:

        private static void httpGet(){
            BufferedReader br = null;
            HttpURLConnection conn = null;
            try {
                String strUrl = "http://agneshome.www.leyingtt.com/agnes_home/config/query";
                URL url = new URL(strUrl);
                conn = (HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setConnectTimeout(10000);
                conn.connect();
    
                br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while(true){
                    line = br.readLine();
                    if(line == null){
                        break;
                    }
                    sb.append(line).append("
    ");
                }
                if(sb.length() > 0){
                    sb.setLength(sb.length()-1);
                }
                Log.i(MainActivity.TAG, sb.toString());
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try{
                    if(br != null){
                        br.close();
                    }
                    if(conn != null){
                        conn.disconnect();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    post请求:

        private static void httpPost(){
            BufferedReader br = null;
            HttpURLConnection conn = null;
            try {
                String strUrl = "https://www.baidu.com";
                URL url = new URL(strUrl);
                conn = (HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("POST");
                conn.connect();
    
                br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while(true){
                    line = br.readLine();
                    if(line == null){
                        break;
                    }
                    sb.append(line).append("
    ");
                }
                if(sb.length() > 0){
                    sb.setLength(sb.length()-1);
                }
                Log.i(MainActivity.TAG, sb.toString());
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try{
                    if(br != null){
                        br.close();
                    }
                    if(conn != null){
                        conn.disconnect();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
  • 相关阅读:
    perl 实现ascall 码转换
    perl 利用管道读取压缩文件内容
    perl 字符串比较操作符
    perl chomp 函数的真正作用
    RSQLite 操作sqlite数据库
    R 中的do.call 函数
    JavaMail发送和接收邮件API(详解)
    POP3_使用SSL链接邮箱并获取邮件
    MySql_delete同时删除多表相关联记录
    mybatis_mybatis写mapper文件注意事项
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/7100466.html
Copyright © 2011-2022 走看看