liuqu

  String URL ="www.baidu.com";

  HttpClient http = new HttpClient();        //创建http连接,                  相当于,打开浏览器

  PostMethod post = new PostMethod(URL);    //创建制定URL地址的PostMethod对象并值定,   相当于在浏览器上输入网址

  String result = "";

  try {

    RequestEntity re = new StringRequestEntity(json.toString(), "application/json", "UTF-8");     //设置请求体(数据,格式,编码)

    post.setRequestEntity(re);

    post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  //使用默认的恢复策略

    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10*1000);                  //设置超时时间

    if(http.executeMethod(post)==200){                    //响应正常

      String result = post.getResponseBodyAsString()             //获取数据

      result = new String(result.getBytes("ISO-8859-1"), "UTF-8");

    }

  }catch(Exception e){

    e.printStackTrace();

  }finally{    

    if(post!=null){
      post.releaseConnection();
    }

  }

  

分类:

技术点:

相关文章:

  • 2021-08-06
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2021-12-12
  • 2021-11-11
  • 2021-12-06
相关资源
相似解决方案