HttpClient使用学习

HttpClient使用学习

HttpClient是Apache HttpComponents中的一部分,下载地址:http://hc.apache.org/

转载:https://www.cnblogs.com/yangchongxing/p/9191073.html

1、简单的Get请求

import java.io.IOException;
import org.apache.http.HttpEntity;
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 Test {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://www.163.com");
        CloseableHttpResponse response = httpClient.execute(httpget);
        int code = response.getStatusLine().getStatusCode();
        System.out.println(code);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity, "UTF-8");
        System.out.println(content);
        response.close();
    }
}

猜你喜欢

转载自www.cnblogs.com/yangchongxing/p/9191073.html
今日推荐