HttpClient简单示例

代码中用到的是httpclient4.3.1和httpcore4.3.1,可以在search.maven.org上找到相应的jar和API。

先登录,然后访问指定URL,打印出服务器反馈的HTML源码。实际应用中可以使用jsoup解析获得的HTML源码。

jsoup参考资料:jsoup Cookbook(中文版)

 

 简单的访问一个URL,获取响应文本。访问之前先登录。

public class SimpleExample {
	/** 要访问的网址 */
	private static final String BASE_URL = "http://hostname:8080/jenkins/";
	private static final String LOGIN_URL = BASE_URL + "/j_acegi_security_check";
	private static final String USERNAME = "username";
	private static final String PASSWORD = "password";

	private final CloseableHttpClient httpclient = HttpClientBuilder.create().build();

	private boolean login() {
		HttpPost httpPost = new HttpPost(LOGIN_URL);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("j_username", USERNAME));
		nvps.add(new BasicNameValuePair("j_password", PASSWORD));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			httpclient.execute(httpPost);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpPost.abort();
		}
		return true;
	}

	private String getText(String url) {
		HttpGet httpget = new HttpGet(url);
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpget, new BasicResponseHandler());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	private void run() throws Exception {
		if (login()) {
			System.out.println(getText(BASE_URL));
		}
	}

	public static void main(String[] args) throws Exception {
		new SimpleExample().run();
	}
}

 

 另一个例子是在内网环境中通过代理服务器访问外网URL,代理服务器要求windows域验证。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
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.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HttpClientHelper {
	private static final Logger logger = LogManager.getLogger(HttpClientHelper.class);
	/**
	 * 获取一个设置了代理服务器的HttpClient对象,适用于内网中的机器通过代理上外网的情况。
	 * 如果机器加入了windows域,可能需要传入workstation、domain这两个参数。
	 * @param proxyHost
	 *            代理服务器IP或主机名
	 * @param proxyPort
	 *            代理服务器端口号
	 * @param proxyUsername
	 *            代理服务器用户名,可为空
	 * @param proxyPassword
	 *            代理服务器密码,仅当代理服务器用户名为空时可为空
	 * @param workstation
	 *            windows主机名
	 * @param domain
	 *            windows域名
	 * @return
	 */
	public CloseableHttpClient getHttpClientWithProxy(String proxyHost,
			int proxyPort, String proxyUsername, String proxyPassword,
			String workstation, String domain) {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		HttpHost host = new HttpHost(proxyHost, proxyPort);
		RequestConfig.Builder reqBuilder  = RequestConfig.custom();
		// 设置连接请求超时时间,防止因服务器响应慢导致异常
		reqBuilder.setConnectionRequestTimeout(180000);
		// 设置连接超时时间,防止文件较大网速较慢时导致异常
		reqBuilder.setSocketTimeout(1000 * 60 * 60);
		httpClientBuilder.setDefaultRequestConfig(reqBuilder.build());
		httpClientBuilder.setProxy(host);
		if (proxyUsername != null) {
			BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			Credentials credentials;
			if (workstation != null) {
				credentials = new NTCredentials(proxyUsername, proxyPassword,
						workstation, domain);
			} else {
				credentials = new UsernamePasswordCredentials(proxyUsername,
						proxyPassword);
			}
			credentialsProvider.setCredentials(new AuthScope(proxyHost,
					proxyPort), credentials);
			httpClientBuilder
					.setDefaultCredentialsProvider(credentialsProvider);
		}
		CloseableHttpClient httpClient = httpClientBuilder.build();
		return httpClient;
	}

	/**
	 * 访问指定的URL,返回字符串形式的响应信息(例如HTML文件内容)
	 * 
	 * @param httpClient
	 * @param url
	 *            要访问的URL
	 * @return
	 */
	public String getText(CloseableHttpClient httpClient, String url) {
		HttpGet httpget = new HttpGet(url);
		String responseBody = null;
		try {
			responseBody = httpClient.execute(httpget,
					new BasicResponseHandler());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	/**
	 * 下载文件到本地
	 * @param httpClient
	 * @param url 文件下载链接
	 * @param localFileFullname 本地文件名(全路径)
	 * @return 下载的字节数
	 */
	public int downloadFile(CloseableHttpClient httpClient, String url,
			String localFileFullname) {
		logger.debug("准备下载" + url);
		int byteCount = 0;
		HttpGet httpget = new HttpGet(url);
		CloseableHttpResponse response;
		InputStream in = null;
		FileOutputStream out = null;
		try {
			DecimalFormat df = new DecimalFormat("#,###");
			response = httpClient.execute(httpget);
			HttpEntity entity = response.getEntity();
			long totalBytes = entity.getContentLength();
			logger.debug("\t该文件大小为" + df.format(totalBytes) + "字节。");
			out = new FileOutputStream(localFileFullname);
			byte[] cache = new byte[4096];
			int count = 0;
			in = entity.getContent();
			do {
				count = in.read(cache);
				if (count > 0) {
					out.write(cache, 0, count);
					byteCount += count;
				}
			} while (count > 0);
			if (byteCount == totalBytes)
				logger.debug("\t下载完毕,文件已保存到" + localFileFullname);
			else
				logger.debug("\t下载失败,当前下载" + df.format(byteCount) + "字节。");
			in.close();
			out.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return byteCount;
	}
}

 

你可能感兴趣的:(HttpClient简单示例)