All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wicp.tams.common.http.HttpClient Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
package net.wicp.tams.common.http;

import java.util.concurrent.FutureTask;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import com.alibaba.fastjson.JSONObject;

import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.IOUtil;
import net.wicp.tams.common.apiext.StringUtil;

public abstract class HttpClient {

	public static final String ENCODE = "UTF-8";
	public static final String HEAD_TYPE = "application/json";
	public static final String CONTENT_TYPE = "text/json";

	public static HttpResult doGet(String url) {
		HttpRequestBase rq = new HttpGet(url);
		HttpThread ht = new HttpThread(rq);
		HttpResult result = ht.call();
		return result;
	}

	public static FutureTask doGetAsyn(String url) {
		HttpRequestBase rq = new HttpGet(url);
		HttpThread ht = new HttpThread(rq);
		return ht.callAsyn();
	}

	public static HttpResult doPost(String url, JSONObject param, Header... headers) {
		HttpThread ht = doPostCommon(url, param.toJSONString(), headers);
		HttpResult result = ht.call();
		return result;
	}

	public static FutureTask doPostAsyn(String url, JSONObject param, Header... headers) {
		HttpThread ht = doPostCommon(url, param.toJSONString(), headers);
		return ht.callAsyn();
	}

	public static HttpThread doPostCommon(String url, String param, Header... headers) {
		HttpPost rq = new HttpPost(url);
		rq.addHeader(HTTP.CONTENT_TYPE, HEAD_TYPE);
		if (ArrayUtils.isNotEmpty(headers)) {
			for (Header header : headers) {
				rq.addHeader(header);
			}
		}
		StringEntity se = new StringEntity(param, ENCODE);
		se.setContentType(CONTENT_TYPE);
		se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, HEAD_TYPE));
		rq.setEntity(se);
		HttpThread ht = new HttpThread(rq);
		return ht;
	}

	public static HttpThread doPostXml(String url, String param, Header... headers) {
		String headType = "application/xml";
		HttpPost rq = new HttpPost(url);
		rq.addHeader(HTTP.CONTENT_TYPE, headType);
		if (ArrayUtils.isNotEmpty(headers)) {
			for (Header header : headers) {
				rq.addHeader(header);
			}
		}
		if (StringUtil.isNotNull(param)) {
			StringEntity se = new StringEntity(param, ENCODE);
			// se.setContentType(CONTENT_TYPE);
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, headType));
			rq.setEntity(se);
		}
		HttpThread ht = new HttpThread(rq);
		return ht;
	}

	/***
	 * 返回真正的URL
	 * 
	 * @param relaUrl
	 * @return
	 */
	public static String packurl(String relaUrl) {
		return IOUtil.mergeFolderAndFilePath(Conf.get("common.http.url.pre"), relaUrl);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy