zoukankan      html  css  js  c++  java
  • HttpClient,get请求,发送并接收数据

           HttpClient http = new HttpClient();                       //创建HttpClient实例,类似于打开浏览器

         GetMethod get = new GetMethod("www.baidu.com");             //创建GET方法的实例,类似于在浏览器地址栏,输入URL
         get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());//使用系统默认的恢复策略

         get.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10*1000);   //设置超时时间
         //http.getHttpConnectionManager().getParams().setConnectionTimeout(10*1000); //设置超时时间 (可选)

         String result = "";
         try {                                          execute是执行的意思
           if(http.executeMethod(g)==200){      //execute执行g方法,类似于点击回车键,向浏览器发出请求          
             result = get.getResponseBodyAsString();//读取浏览器返回的结果byte[] responseBody = getMethod.getResponseBody(); 
             result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
           }else{
             result="{"code":"1111","innercode":"1111","message":"连接错误"}";
           }
         } catch (Exception e) {
           result="{"code":"1111","innercode":"1111","message":"连接超时"}";
         } finally{
           if(get!=null){
             get.releaseConnection();                       //告诉HttpClient,这个链接,可以重复使用
           }
         }

  • 相关阅读:
    【python】+json+解析数组json字符串(且键没有引号)(完美解决)
    【Python】+类内部方法相互调用
    【Python】+字符串转换为日期(互转)+获取当前时间+获取当前时间戳
    【python】+tushare库+判断指定日期是否是交易日
    【python】+占位符
    【python】【pycharm】+代码自动提示
    【python】+命名规范
    【python】+'chromedriver' executable needs to be in PATH
    【python】+8大数据类型
    【python】+字典操作(全)
  • 原文地址:https://www.cnblogs.com/liuqu/p/8609769.html
Copyright © 2011-2022 走看看