Httpclient基本操作

import java.io.File;  
import org.apache.commons.io.FileUtils;  
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 DoGET {  
  
    public static void main(String[] args) throws Exception {  
  
        // 创建Httpclient对象  
        CloseableHttpClient httpclient = HttpClients.createDefault();  
  
        // 创建http GET请求  
        HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");  
        CloseableHttpResponse response = null;  
        try {  
            // 执行请求  
            response = httpclient.execute(httpGet);  
            // 判断返回状态是否为200  
            if (response.getStatusLine().getStatusCode() == 200) {  
                // 获取服务端返回的数据  
                String content = EntityUtils.toString(response.getEntity(),  
                        "UTF-8");  
                FileUtils.writeStringToFile(new File("E:\\baidu.html"),  
                        content, "UTF-8");  
                // 服务端返回数据的长度  
                System.out.println("内容长度:" + content.length());  
            }  
        } finally {  
            if (response != null) {  
                response.close();  
            }  
            // 相当于关闭浏览器  
            httpclient.close();  
        }  
  
    }  
  
}  


import java.net.URI;  
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.utils.URIBuilder;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.util.EntityUtils;  
  
public class DoGETParam {  
  
    public static void main(String[] args) throws Exception {  
  
        // 创建Httpclient对象  
        CloseableHttpClient httpclient = HttpClients.createDefault();  
  
        // 定义请求的参数  
        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd",  
                "java").build();  
  
        System.out.println(uri);  
  
        // 创建http GET请求  
        HttpGet httpGet = new HttpGet(uri);  
  
        CloseableHttpResponse response = null;  
        try {  
            // 执行请求  
            response = httpclient.execute(httpGet);  
            // 判断返回状态是否为200  
            if (response.getStatusLine().getStatusCode() == 200) {  
                // 获取服务端,响应的数据  
                String content = EntityUtils.toString(response.getEntity(),  
                        "UTF-8");  
                System.out.println(content);  
            }  
        } finally {  
            if (response != null) {  
                response.close();  
            }  
            httpclient.close();  
        }  
  
    }  
  
}  


import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.util.EntityUtils;  
  
public class DoPOST {  
  
    public static void main(String[] args) throws Exception {  
  
        // 创建Httpclient对象  
        CloseableHttpClient httpclient = HttpClients.createDefault();  
  
        // 创建http POST请求  
        HttpPost httpPost = new HttpPost("http://www.oschina.net/");  
        // 伪装浏览器请求  
        httpPost.setHeader(  
                "User-Agent",  
                "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");  
  
        CloseableHttpResponse response = null;  
        try {  
            // 执行请求  
            response = httpclient.execute(httpPost);  
            // 判断返回状态是否为200  
            if (response.getStatusLine().getStatusCode() == 200) {  
                // 获取服务端,响应的数据  
                String content = EntityUtils.toString(response.getEntity(),  
                        "UTF-8");  
                System.out.println(content);  
            }  
        } finally {  
            if (response != null) {  
                response.close();  
            }  
            httpclient.close();  
        }  
  
    }  
  
}  

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class DoPOSTParam {

public static void main(String[] args) throws Exception {  

    // 创建Httpclient对象  
    CloseableHttpClient httpclient = HttpClients.createDefault();  

    // 创建http POST请求  
    HttpPost httpPost = new HttpPost("http://www.oschina.net/search");  

    // 设置2个post参数,一个是scope、一个是q  
    List parameters = new ArrayList();  
    parameters.add(new BasicNameValuePair("scope", "project"));  
    parameters.add(new BasicNameValuePair("q", "java"));  
    // 构造一个form表单式的实体  
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);  
    // 将请求实体设置到httpPost对象中  
    httpPost.setEntity(formEntity);  
    // 伪装浏览器请求  
    httpPost.setHeader(  
            "User-Agent",  
            "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");  

    CloseableHttpResponse response = null;  
    try {  
        // 执行请求  
        response = httpclient.execute(httpPost);  
        // 判断返回状态是否为200  
        if (response.getStatusLine().getStatusCode() == 200) {  
            // 获取服务端响应的数据  
            String content = EntityUtils.toString(response.getEntity(),  
                    "UTF-8");  
            System.out.println(content);  
        }  
    } finally {  
        if (response != null) {  
            response.close();  
        }  
        httpclient.close();  
    }  

}  

}

import org.apache.http.client.config.RequestConfig;
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 RequestConfigDemo {

public static void main(String[] args) throws Exception {  

    // 创建Httpclient对象  
    CloseableHttpClient httpclient = HttpClients.createDefault();  

    // 创建http GET请求  
    HttpGet httpGet = new HttpGet("http://www.baidu.com/");  

    // 构建请求配置信息  
    RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间  
            .setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间  
            .setSocketTimeout(10 * 1000) // 数据传输的最长时间  
            .setStaleConnectionCheckEnabled(true) // 提交请求前测试连接是否可用  
            .build();  
    // 设置请求配置信息  
    httpGet.setConfig(config);  

    CloseableHttpResponse response = null;  
    try {  
        // 执行请求  
        response = httpclient.execute(httpGet);  
        // 判断返回状态是否为200  
        if (response.getStatusLine().getStatusCode() == 200) {  
            String content = EntityUtils.toString(response.getEntity(),  
                    "UTF-8");  
            System.out.println(content);  
        }  
    } finally {  
        if (response != null) {  
            response.close();  
        }  
        httpclient.close();  
    }  

}  

}

你可能感兴趣的:(Httpclient基本操作)