长话短说,废话不说一、第一种方式,通过HttpClient方式,代码如下:public static String httpGet(String url, String charset) throws HttpException, IO
长话短说,废话不说
一、第一种方式,通过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;}
--结束END--
本文标题: java发送http get请求的两种方式
本文链接: https://www.lsjlt.com/news/226038.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0