HttpClient 开发示例HttpGet和HttpPost

2020-10-10 0 By admin

HttpClient 为第三方Http 客户端类库;在开发过程中主要是发送Http协议的Post请求和Get请求。其中Post请求使用HttpPost对象构建、Get请求使用HttpGet对象构建。

一、HttpGet 请求

public static void main(String[] args) throws Exception {
	// 格式化请求url
    // String cmd=String.format("http://%s:%s@%s",username,password,split[1]);
	
	// 请求地址
    String url = "https://blog.csdn.net/amosjob/article/details/82775290";
    // 创建默认的客户端实例  
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建get请求实例 
    HttpGet httpGet = new HttpGet(url);
    // 设置头部信息
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
//  httpGet.addHeader("x-api-key", "XXX");
//  httpGet.addHeader("x-lang", "en-US");
    // 执行GET请求 返回响应实体 
    HttpResponse response = httpclient.execute(httpGet);
    // 获取响应码
    int statusCode = response.getStatusLine().getStatusCode();
    // 获取全部的请求头
    Header[] allHeaders = response.getAllHeaders();
    System.out.println("响应状态码:" + statusCode);
    for (int i = 0; i < allHeaders.length; i++) {
    	 System.out.println("全部的请求头:" + allHeaders[i]);
	}
    // 获取响应消息实体  
    HttpEntity entity = response.getEntity();
    //打印输出网页的内容(可删除)
    if (entity != null) {
    	// 推荐该办法打印内容
    	System.out.println("响应的内容:"+EntityUtils.toString(entity));
    	
    	// 也可以使用这个方法
        InputStream inputStream = entity.getContent();
        InputStreamReader streamReader = new InputStreamReader(inputStream);
        int line;
        line = streamReader.read() ;
        while (line!= -1) {
            System.out.print((char)line);
            line = streamReader.read() ;
        }
    }
	// 关闭连接
	try {
		httpclient.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

二、HttpPost 请求

public static void main(String[] args) throws Exception {
	// 请求地址
	String url = "https://passport.csdn.net/account/login";
	// 创建默认的客户端实例
	CloseableHttpClient httpclient = HttpClients.createDefault();
	// 创建POST对象
	HttpPost httpPost = new HttpPost(url);
	// 传参数
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	params.add(new BasicNameValuePair("username", "admin"));
	params.add(new BasicNameValuePair("password", "12345"));
	try {
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
		httpPost.setEntity(entity);
	} catch (Exception e) {
		e.printStackTrace();
	}
	// 执行GET请求 返回响应实体
	HttpResponse response = httpclient.execute(httpPost);
	// 获取响应码
	int statusCode = response.getStatusLine().getStatusCode();
	// 获取全部的请求头
	Header[] allHeaders = response.getAllHeaders();
	System.out.println("响应状态码:" + statusCode);
	for (int i = 0; i < allHeaders.length; i++) {
		System.out.println("全部的请求头:" + allHeaders[i]);
	}
	// 获取响应消息实体
	HttpEntity entity = response.getEntity();
	// 打印输出网页的内容(可删除)
	if (entity != null) {
		System.out.println("响应的内容:"+EntityUtils.toString(entity));
	}
	// 关闭连接
	try {
		httpclient.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}