长话短说,废话不说
一、第一种方式,通过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。内容未经允许不得转载,或转载时需注明来源: 创新互联