标签:content 模型 创建 nbsp pcl 支持 system ringbuf org
通俗的讲httpClient就是 模拟浏览器向某个网址发送各种请求
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency>
1、发送Get请求,无参
public void test() { //1、得到httpClient的客户端,相当于打开了一个浏览器窗口 CloseableHttpClient httpclient = HttpClientBuilder.create().build(); //2、创建一个get请求,相当于你在浏览器窗口输入了一个网址 HttpGet httpGet = new HttpGet("http://www.baidu.com"); //3、创建一个相应模型的对象,用于接收服务器响应的数据 CloseableHttpResponse httpResponse = null; try { //4、发送请求,相当于你敲了一下回车 httpResponse = httpclient.execute(httpGet); //5、获取http状态 System.out.println("状态:" +httpResponse.getStatusLine()); //6、从响应模型获取响应体 HttpEntity entity = httpResponse.getEntity(); if(entity != null) { System.out.println("响应长度"+entity.getContentLength()); System.out.println("响应数据"+EntityUtils.toString(entity,"UTF-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { //倒序关闭资源 try { if(httpResponse != null) { httpResponse.close(); } if(httpclient != null) { httpclient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
2、发送Get请求, 带参数
public void testdoGetRequest2() { // 1.得到HttpClient的客户端,相当于你打开了一个浏览器窗口 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 参数 StringBuffer params = new StringBuffer(); try { // 字符数据最好进行url编码;这样一来,某些特殊字符才能传过去(如:参数就是“&”,不url编码,传不过去) params.append("wd=" + URLEncoder.encode("张三丰", "utf-8")); params.append("&"); params.append("rsv_spt=1"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } // 2.创建一个get请求, 相当于你在浏览器窗口输入了一个网址 HttpGet httpGet = new HttpGet("http://www.baidu.com/s?"+params.toString()); // 3.创建一个响应模型对象,用于接收服务器响应的数据 CloseableHttpResponse httpResponse = null; try { // 4.发送请求, 相当于你敲了一下回车 httpResponse = httpClient.execute(httpGet); // 5.获取Http状态 System.out.println("状态:" + httpResponse.getStatusLine()); // 6.从响应模型获取响应实体 HttpEntity entity = httpResponse.getEntity(); if (entity != null) { System.out.println("响应长度:" + entity.getContentLength()); System.out.println("响应数据:" + EntityUtils.toString(entity, "UTF-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
3、发送Get请求, 使用uri关键访问的路径
public void testdoGetRequest3() { // 1.得到HttpClient的客户端,相当于你打开了一个浏览器窗口 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 构建uri URI uri = null; try { //将参数放入键值对类NameValuePair中,再放入集合中 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("wd", "java")); params.add(new BasicNameValuePair("rsv_bp", "1")); // 设置uri信息,并将参数集合放入uri; // 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value) uri = new URIBuilder().setScheme("http").setHost("www.baidu.com").setPort(80).setPath("/s") .setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } // 2.创建一个get请求, 相当于你在浏览器窗口输入了一个网址 HttpGet httpGet = new HttpGet(uri ); // 3.创建一个响应模型对象,用于接收服务器响应的数据 CloseableHttpResponse httpResponse = null; try { // 4.发送请求, 相当于你敲了一下回车 httpResponse = httpClient.execute(httpGet); // 5.获取Http状态 System.out.println("状态:" + httpResponse.getStatusLine()); // 6.从响应模型获取响应实体 HttpEntity entity = httpResponse.getEntity(); if (entity != null) { System.out.println("响应长度:" + entity.getContentLength()); System.out.println("响应数据:" + EntityUtils.toString(entity, "UTF-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
4、发送Post请求,无参
public void testdoPostRequest1() { // 1.得到HttpClient的客户端,相当于你打开了一个浏览器窗口 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 2.创建一个post请求, 相当于你在浏览器窗口输入了一个网址 HttpPost httpPost = new HttpPost("http://www.baidu.com"); // 3.创建一个响应模型对象,用于接收服务器响应的数据 CloseableHttpResponse httpResponse = null; try { // 4.发送请求, 相当于你敲了一下回车 httpResponse = httpClient.execute(httpPost); // 5.获取Http状态 System.out.println("状态:" + httpResponse.getStatusLine()); // 6.从响应模型获取响应实体 HttpEntity entity = httpResponse.getEntity(); if (entity != null) { System.out.println("响应长度:" + entity.getContentLength()); System.out.println("响应数据:" + EntityUtils.toString(entity, "UTF-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
5、发送Post请求,带参
public void testdoPostRequest2() { // 1.得到HttpClient的客户端,相当于你打开了一个浏览器窗口 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 2.创建一个post请求, 相当于你在浏览器窗口输入了一个网址 // 构建uri URI uri = null; try { // 将参数放入键值对类NameValuePair中,再放入集合中 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("Uid", "本站用户名")); params.add(new BasicNameValuePair("Key", "接口安全秘钥")); params.add(new BasicNameValuePair("smsMob","手机号码")); params.add(new BasicNameValuePair("smsText","验证码:8888【XX公司或XX网名称】")); // 设置uri信息,并将参数集合放入uri; // 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value) uri = new URIBuilder().setScheme("http").setHost("utf8.api.smschinese.cn") .setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } HttpPost httpPost = new HttpPost(uri); // 3.创建一个响应模型对象,用于接收服务器响应的数据 CloseableHttpResponse httpResponse = null; try { // 4.发送请求, 相当于你敲了一下回车 httpResponse = httpClient.execute(httpPost); // 5.获取Http状态 System.out.println("状态:" + httpResponse.getStatusLine()); // 6.从响应模型获取响应实体 HttpEntity entity = httpResponse.getEntity(); if (entity != null) { System.out.println("响应长度:" + entity.getContentLength()); System.out.println("响应数据:" + EntityUtils.toString(entity, "UTF-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
标签:content 模型 创建 nbsp pcl 支持 system ringbuf org
原文地址:https://www.cnblogs.com/64Byte/p/13189563.html