HttpClient 实例
博客专区 > 诺岚 的博客 > 博客详情
HttpClient 实例
诺岚 发表于3个月前
HttpClient 实例
  • 发表于 3个月前
  • 阅读 14
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

1.百度例子:

import org.apache.http.Header;
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;

public class HttpClientDemo02 {
    public static void main(String[] args) throws Exception {
        //创建实例化对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建get请求实例
        HttpGet httpget = new HttpGet("http://www.baidu.com");
        System.out.println("executing request " + httpget.getURI());
        // 客户端执行get请求 返回响应
        CloseableHttpResponse response = httpClient.execute(httpget);
        // 服务器响应状态行
        System.out.println("服务器响应状态行"+response.getStatusLine().toString());
        Header[] heads = response.getAllHeaders();
        System.out.println(response.getHeaders("Content-Type"));
        // 打印所有响应头
        System.out.println("打印所有响应头");
        for (Header h : heads) {
            System.out.println(h.getName() + ":" + h.getValue());
        }
        httpClient.close();
    }

}

运行结果:

executing request http://www.baidu.com
服务器响应状态行HTTP/1.1 200 OK
[Lorg.apache.http.Header;@13deb50e
打印所有响应头
Server:bfe/1.0.8.18
Date:Wed, 30 Aug 2017 03:03:45 GMT
Content-Type:text/html
Last-Modified:Mon, 23 Jan 2017 13:27:57 GMT
Transfer-Encoding:chunked
Connection:Keep-Alive
Cache-Control:private, no-cache, no-store, proxy-revalidate, no-transform
Pragma:no-cache
Set-Cookie:BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

Process finished with exit code 0

 

共有 人打赏支持
粉丝 0
博文 62
码字总数 101899
×
诺岚
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: