HttpClient HelloWorld实现

HttpClient HelloWorld实现

前面我们介绍了HttpClient 这个框架主要用来请求第三方服务器,然后获取到网页,得到我们需要的数据;

所以今天搞个简单实例,让大家体验一把。

首先建一个Maven项目,然后添加httpClient依赖,版本是4.5

1
2
3
4
5
<dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version> 4.5 . 2 </version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package  com.open1111.httpclient;
 
import  java.io.IOException;
 
import  org.apache.http.HttpEntity;
import  org.apache.http.ParseException;
import  org.apache.http.client.ClientProtocolException;
import  org.apache.http.client.methods.CloseableHttpResponse;
import  org.apache.http.client.methods.HttpGet;
import  org.apache.http.impl.client.CloseableHttpClient;
import  org.apache.http.impl.client.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  HelloWorld {
 
     public  static  void  main(String[] args) {
         CloseableHttpClient httpClient=HttpClients.createDefault();  // 创建httpClient实例
         HttpGet httpget =  new  HttpGet( "http://www.open1111.com/" ); // 创建httpget实例
         CloseableHttpResponse response= null ;
         try  {
             response = httpClient.execute(httpget);
         catch  (ClientProtocolException e) {   // http协议异常
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (IOException e) {  // io异常
             // TODO Auto-generated catch block
             e.printStackTrace();
         // 执行get请求
         HttpEntity entity=response.getEntity();  // 获取返回实体
         try  {
             System.out.println( "网页内容:" +EntityUtils.toString(entity,  "utf-8" ));
         catch  (ParseException e) {   // 解析异常
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (IOException e) {  // io异常
             // TODO Auto-generated catch block
             e.printStackTrace();
         // 指定编码打印网页内容
         try  {
             response.close();
         catch  (IOException e) {   // io异常
             // TODO Auto-generated catch block
             e.printStackTrace();
         // 关闭流和释放系统资源
     }
}

视频里会有详细讲解,运行输出:

QQ鎴浘20170115173034.jpg


这里得到了网站首页源码,当然要获得具体数据的话,要用到Jsoup,我们后面课程会讲解该技术;


假如你对这些异常都熟悉 我们可以简化下,异常抛出,这样代码可读性好点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  com.open1111.httpclient;
 
 
import  org.apache.http.HttpEntity;
import  org.apache.http.client.methods.CloseableHttpResponse;
import  org.apache.http.client.methods.HttpGet;
import  org.apache.http.impl.client.CloseableHttpClient;
import  org.apache.http.impl.client.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  HelloWorld2 {
 
     public  static  void  main(String[] args)  throws  Exception{
         CloseableHttpClient httpclient = HttpClients.createDefault();  // 创建httpclient实例
         HttpGet httpget =  new  HttpGet( "http://www.open1111.com/" ); // 创建httpget实例
         
         CloseableHttpResponse response = httpclient.execute(httpget);  // 执行get请求
         HttpEntity entity=response.getEntity();  // 获取返回实体
         System.out.println( "网页内容:" +EntityUtils.toString(entity,  "utf-8" ));  // 指定编码打印网页内容
         response.close();  // 关闭流和释放系统资源
     }
}



但是实际开发的话,我们对于每一种异常的抛出,catch里都需要做一些业务上的操作,所以以后用的话,还是第一种,假如爬虫任务很简单,容易爬取,并且量小,那就第二种。还是要根据具体情况来。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个C#中使用HttpClient的帮助类,包含了常用的HTTP请求方法和一些常见的配置选项: ```csharp using System; using System.Net.Http; using System.Threading.Tasks; public static class HttpClientHelper { private static readonly HttpClient _httpClient = new HttpClient(); public static async Task<string> GetAsync(string url) { var response = await _httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } public static async Task<byte[]> GetByteArrayAsync(string url) { var response = await _httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(); } public static async Task<string> PostAsync(string url, HttpContent content) { var response = await _httpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } public static async Task<byte[]> PostByteArrayAsync(string url, HttpContent content) { var response = await _httpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(); } public static async Task<string> PutAsync(string url, HttpContent content) { var response = await _httpClient.PutAsync(url, content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } public static async Task<byte[]> PutByteArrayAsync(string url, HttpContent content) { var response = await _httpClient.PutAsync(url, content); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(); } public static async Task<string> DeleteAsync(string url) { var response = await _httpClient.DeleteAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } public static async Task<byte[]> DeleteByteArrayAsync(string url) { var response = await _httpClient.DeleteAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(); } public static void SetTimeout(TimeSpan timeout) { _httpClient.Timeout = timeout; } public static void SetBaseAddress(string baseAddress) { _httpClient.BaseAddress = new Uri(baseAddress); } public static void SetDefaultRequestHeaders(Action<HttpRequestHeaders> action) { action(_httpClient.DefaultRequestHeaders); } } ``` 使用示例: ```csharp // 设置基地址 HttpClientHelper.SetBaseAddress("http://localhost:9000/"); // 设置超时时间 HttpClientHelper.SetTimeout(TimeSpan.FromSeconds(10)); // 设置默认请求头 HttpClientHelper.SetDefaultRequestHeaders(headers => { headers.Add("User-Agent", "HttpClientHelper"); }); // 发送GET请求 var response = await HttpClientHelper.GetAsync("index.html"); var content = await response.Content.ReadAsStringAsync(); // 发送POST请求 var content = new StringContent("Hello, world!"); var response = await HttpClientHelper.PostAsync("api/values", content); var result = await response.Content.ReadAsStringAsync(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值