2018-06-15: Java WebClient - HttpClient - 根据WebAPI请求数据

自定义一个 http 请求类,常用于 java 后台代码根据URL地址 获取数据

------------------------------ BEGIN ----------------------------------

1、向 WebAPI 的url 提交数据 - POST 方式

a、具体方法:

/**
 * @Title : httpPostJson
 * @Function: HTTPPost发送JSON
 * @param url
 * @param jsonStr
 * @throws IOException
 * @throws ClientProtocolException
 */
public static String httpPostJson(String url, String jsonStr) throws ClientProtocolException, IOException {
	String resultStr = null;
	
	// 创建一个DefaultHttpClient的实例
	HttpClient httpClient = new DefaultHttpClient();
	// 创建一个HttpPost对象,传入目标的网络地址
	HttpPost httpPost = new HttpPost(url);
	//httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
	//这里要这样设置...C#的接口需要
	httpPost.setHeader("contentType", "application/json;charset=UTF-8");

	// 提交的数据 - 注明utf8编码
	StringEntity se = new StringEntity(jsonStr, "UTF-8");
	se.setContentType("text/json");
	//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
	//这里要这样设置...C#的接口需要
	se.setContentEncoding(new BasicHeader("contentType", "application/json"));
	// 传入参数
	httpPost.setEntity(se);
	
	// 调用HttpClient的execute()方法,并将HttpPost对象传入;
	// 执行execute()方法之后会返回一个HttpResponse对象,服务器所返回的所有信息就保护在HttpResponse里面
	HttpResponse response = httpClient.execute(httpPost);

	// 输出调用结果 - 
	if(null != response && null != response.getStatusLine()){
		//先取出服务器返回的状态码,如果等于200就说明请求和响应都成功了
		if(response.getStatusLine().getStatusCode() == 200){
			// 调用getEntity()方法获取到一个HttpEntity实例
			// EntityUtils.toString()这个静态方法将HttpEntity转换成字符串,防止服务器返回的数据带有中文,所以在转换的时候将字符集指定成utf-8
			resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
		}else{
			System.out.println("HttpResponse StatusCode:" + response.getStatusLine().getStatusCode());
		}
	}

	return resultStr;
}
复制代码

b、测试方法:

String result = httpPostJson(url, paramsStr);


2、这是一个比较复杂的类、常用语GET数据,POST也行 WebClient

package com.aaa.bbb.ccc.ddd;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class WebClient {
	private DefaultHttpClient httpClient = new DefaultHttpClient();
	private String url;
	private HTTPMethod method;
	private byte[] content;
	private Map<String, String> headers = new HashMap<String, String>();
	private int responseCode;
	private List<NameValuePair> postParameter = new ArrayList<NameValuePair>();

	private static final Pattern pageEncodingReg = Pattern.compile("content-type.*charset=([^\">\\\\]+)",Pattern.CASE_INSENSITIVE);
	private static final Pattern headerEncodingReg = Pattern.compile("charset=(.+)", Pattern.CASE_INSENSITIVE);

	public static void main(String[] args) throws Exception {
		WebClient web = new WebClient("http://localhost:8081/test/api?s=1",HTTPMethod.GET);
		// web.enableProxy("10.10.10.10", 8080, false, null, null,"127.0.0.1");
		System.out.println(web.getTextContent());
		System.out.println("------------------------------------------");
		
		/**
		 * web.setUrl("https://mail.google.com/mail/"); 
		 * System.out.println(web.getTextContent());
		 * System.out.println("------------------------------------------");
		 * web.setUrl("http://www.snee.com/xml/crud/posttest.cgi");
		 * web.setMethod(HTTPMethod.POST);
		 * web.addPostParameter("fname", "ababab");
		 * web.addPostParameter("lname", "cdcdcd");
		 * System.out.println(web.getTextContent());
		 * System.out.println("------------------------------------------");
		 */
	}

	public WebClient(String url, HTTPMethod method) {
		this(url, method, false, null, 0, false, null, null, null);
	}

	public WebClient(String url, HTTPMethod method, String proxyHost, int proxyPort) {
	    this(url, method, true, proxyHost, proxyPort, false, null, null, null);
	}

	public WebClient(String url, HTTPMethod method, boolean useProxy, String proxyHost, int proxyPort, boolean needAuth, String username, String password, String nonProxyReg) {
	    setUrl(url);
	    setMethod(method);
	    if (useProxy) {
	    	enableProxy(proxyHost, proxyPort, needAuth, username, password,nonProxyReg);
            }
	}

	public void setMethod(HTTPMethod method) {
		this.method = method;
	}

	public void setUrl(String url) {
		if (isStringEmpty(url)) {
			throw new RuntimeException("Url不能为空.");
		}
		this.url = url;
		headers.clear();
		responseCode = 0;
		postParameter.clear();
		content = null;
		if (url.startsWith("https://")) {
			enableSSL();
		} else {
			disableSSL();
		}
	}

	public Map<String, String> getRequestHeaders() {
		return headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public void addPostParameter(String name, String value) {
		this.postParameter.add(new BasicNameValuePair(name, value));
	}

	public void setTimeout(int connectTimeout, int readTimeout) {
		HttpParams params = httpClient.getParams();
		HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
		HttpConnectionParams.setSoTimeout(params, readTimeout);
	}

	private void enableSSL() {
		try {
			SSLContext sslcontext = SSLContext.getInstance("TLS");
			sslcontext.init(null, new TrustManager[] {truseAllManager}, null);
			SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			// sf.setHostnameVerifier();
			Scheme https = new Scheme("https", 443, sf);
			httpClient.getConnectionManager().getSchemeRegistry().register(https);
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	private void disableSSL() {
		SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
		if (reg.get("https") != null) {
			reg.unregister("https");
		}
	}

	public void disableProxy() {
		httpClient.getCredentialsProvider().clear();
		httpClient.setRoutePlanner(null);
	}

	public void enableProxy(final String proxyHost, final int proxyPort, boolean needAuth, String username, String password, final String nonProxyHostRegularExpression) {
		if (needAuth) { httpClient.getCredentialsProvider()
				.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(username, password));
		}
		// httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,new
		// HttpHost(proxyHost, proxyPort));
		httpClient.setRoutePlanner(new HttpRoutePlanner() {
			@Override
			public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext contenxt) throws HttpException {
				HttpRoute proxyRoute = new HttpRoute(target, null, new HttpHost(proxyHost, proxyPort), "https".equalsIgnoreCase(target.getSchemeName()));
				if (nonProxyHostRegularExpression == null) {
					return proxyRoute;
				}
				Pattern pattern = Pattern.compile(nonProxyHostRegularExpression, Pattern.CASE_INSENSITIVE);
				Matcher m = pattern.matcher(target.getHostName());
				if (m.find()) {
					return new HttpRoute(target, null, target, "https".equalsIgnoreCase(target.getSchemeName()));
				} else {
					return proxyRoute;
				}
			}
		});
	}

	private void fetch() throws IOException {
		if (url == null || method == null) {
			throw new RuntimeException("参数错误....");
		}
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
		HttpResponse response = null;
		HttpUriRequest req = null;
		if (method.equals(HTTPMethod.GET)) {
			req = new HttpGet(url);
		} else {
			req = new HttpPost(url);
			((HttpPost) req).setEntity(new UrlEncodedFormEntity(this.postParameter, HTTP.UTF_8));
		}
		for (Entry<String, String> e : headers.entrySet()) {
			req.addHeader(e.getKey(), e.getValue());
		}
		req.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
		response = httpClient.execute(req);
		Header[] header = response.getAllHeaders();
		headers.clear();
		for (Header h : header) {
			headers.put(h.getName(), h.getValue());
		}
		content = EntityUtils.toByteArray(response.getEntity());
		responseCode = response.getStatusLine().getStatusCode();
	}

	private boolean isStringEmpty(String s) {
		return s == null || s.length() == 0;
	}

	public int getResponseCode() throws IOException {
		if (responseCode == 0) {
			fetch();
		}
		return responseCode;
	}

	public Map<String, String> getResponseHeaders() throws IOException {
		if (responseCode == 0) {
			fetch();
		}
		return headers;
	}

	public byte[] getByteArrayContent() throws IOException {
		if (content == null) {
			fetch();
		}
		return content;
	}

	public String getTextContent() throws IOException {
		if (content == null) {
			fetch();
		}
		if (content == null) {
			throw new RuntimeException("抓取类容错误.");
		}
		String headerContentType = null;
		if ((headerContentType = headers.get("Content-Type")) != null) {
			// use http header encoding
			Matcher m1 = headerEncodingReg.matcher(headerContentType);
			if (m1.find()) {
				return new String(content, m1.group(1));
			}
		}
		String html = new String(content);
		Matcher m2 = pageEncodingReg.matcher(html);
		if (m2.find()) {
			html = new String(content, m2.group(1));
		}
		return html;
	}

	public DefaultHttpClient getHttpClient() {
		return httpClient;
	}

	public enum HTTPMethod {
		GET, POST
	}

	private static TrustManager truseAllManager = new X509TrustManager() {
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
	};
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值