集册 Java实例教程 使用apache http直接执行客户端

使用apache http直接执行客户端

欢马劈雪     最近更新时间:2020-01-02 10:19:05

538
客户端使用apache http直接执行
/*来自 nowjava.com - 时代Java*/

import java.io.IOException;


import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

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 ClientExecuteDirect {


    public static void main(String[] args) {

        try (final CloseableHttpClient hc = HttpClients.createDefault();) {

            HttpHost target = new HttpHost("www.apache.org", 80, "http");

            HttpGet req = new HttpGet("/");/*n o w j a v a . c o m 提供*/

            System.out.println("executing reqeust " + target);


            HttpResponse res = hc.execute(target, req);

            System.out.println("------------------------------------");

            System.out.println(res.getStatusLine());

            Header[] headers = res.getAllHeaders();

            for (Header header : headers) {

                System.out.println(header);

            }

            System.out.println(
展开阅读全文