java发送httpget请求的两种方式-创新互联

长话短说,废话不说

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了宁都免费建站欢迎大家使用!

一、第一种方式,通过HttpClient方式,代码如下:

public static String httpGet(String url, String charset)
   throws HttpException, IOException {
  String json = null;
  HttpGet httpGet = new HttpGet();
  // 设置参数
  try {
   httpGet.setURI(new URI(url));
  } catch (URISyntaxException e) {
   throw new HttpException("请求url格式错误。"+e.getMessage());
  }
  // 发送请求
  HttpResponse httpResponse = client.execute(httpGet);
  // 获取返回的数据
  HttpEntity entity = httpResponse.getEntity();
  byte[] body = EntityUtils.toByteArray(entity);
  StatusLine sL = httpResponse.getStatusLine();
  int statusCode = sL.getStatusCode();
  if (statusCode == 200) {
   json = new String(body, charset);
   entity.consumeContent();
  } else {
   throw new HttpException("statusCode="+statusCode);
  }
  return json;
}

分享题目:java发送httpget请求的两种方式-创新互联
链接地址:https://www.cdcxhl.com/article4/dehgoe.html

成都网站建设公司_创新互联,为您提供服务器托管微信公众号网站策划品牌网站制作网站建设品牌网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化