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请求的两种方式-创新互联
当前地址:http://myzitong.com/article/dehgoe.html