⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpclienthelper.java

📁 用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息
💻 JAVA
字号:
package lib.commons.net.http;

import java.io.IOException;

import lib.commons.net.AuthDomain;
import lib.commons.net.SocketFactFactory;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;

import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.Protocol;

public class HttpClientHelper {
	private HttpMethodType _methodType;

	private HttpClient _httpClient;

	private static HttpClient _simpleHttpClient, _multiThreadHttpClient;

	private HttpMethodBase _httpMethod;

	private AuthDomain _proxy;

	public HttpClientHelper(HttpMethodType methodType, String requestUrl,
			int connectionTimeout, int readTimeout, AuthDomain proxy) {
		this(methodType, requestUrl, connectionTimeout, readTimeout, proxy,
				false);
	}

	public HttpClientHelper(HttpMethodType methodType, String requestUrl,
			int connectionTimeout, int readTimeout, AuthDomain proxy,
			boolean isMultiThread) {
		_methodType = methodType;
		if (isMultiThread) {
			if (null == _multiThreadHttpClient) {
				synchronized (HttpClientHelper.class) {
					if (null == _multiThreadHttpClient)
						_multiThreadHttpClient = new HttpClient(
								new MultiThreadedHttpConnectionManager());
				}
			}
			_httpClient = _multiThreadHttpClient;
		} else {
			if (null == _simpleHttpClient) {
				synchronized (HttpClientHelper.class) {
					if (null == _simpleHttpClient)
						_simpleHttpClient = new HttpClient(
								new SimpleHttpConnectionManager());
				}
			}
			_httpClient = _simpleHttpClient;
		}
		if (connectionTimeout > 0){
			_httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
			//_httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout > 0?Integer.valueOf(connectionTimeout):new Integer(0));
			
		}	
		if (readTimeout > 0)
		{
			_httpClient.getHttpConnectionManager().getParams().setSoTimeout(readTimeout);
			//_httpClient.getParams().setParameter("http.socket.timeout", readTimeout > 0?Integer.valueOf(connectionTimeout):new Integer(0));
		}
		if (null != proxy) {
			_httpClient.getHostConfiguration()
					.setProxy(proxy.getDomain().getAddress(),
							proxy.getDomain().getPort());
		}
		_httpMethod = methodType.getMethodInstance(requestUrl);
	}

	public HttpMethodType getMethodType() {
		return _methodType;
	}

	public HttpClient getHttpClient() {
		return _httpClient;
	}

	public HttpMethodBase getHttpMethod() {
		return _httpMethod;
	}

	public AuthDomain getProxy() {
		return _proxy;
	}

	public HttpResponse execute() throws IOException, HttpException {
		int statusCode = _httpClient.executeMethod(_httpMethod);
		HttpResponse response = new HttpResponse();
		response._statusCode = statusCode;
		response._url = _httpMethod.getURI().toString();
		// response._charset = _httpMethod.getResponseCharSet();
		response._responseBody = _httpMethod.getResponseBody();
		Header[] headers = _httpMethod.getResponseHeaders();
		if (null != headers && headers.length > 0) {
			response._headerNameList = new java.util.ArrayList();
			response._headers = new java.util.HashMap();
			for (int i = 0; i < headers.length; i++) {
				String headerName = headers[i].getName();
				String headerValue = headers[i].getValue();
				response._headerNameList.add(headerName);
				response._headers.put(headerName.toUpperCase(), headerValue);
			}
		}
		return response;
	}

	public void Close() {
		try {
			_httpMethod.releaseConnection();
			//_httpClient.getHttpConnectionManager().closeIdleConnections(100);
		} catch (Exception err) {
			lib.commons.logging.LogFactory.getLog(this.getClass()).error(
					err.getMessage(), err);
		}
	}

	public boolean getFollowRedirects() {
		if (_methodType!=HttpMethodType.POST)
			return _httpMethod.getFollowRedirects();
		else
			return false;
	}

	public void setFollowRedirects(boolean b) {
		if (_methodType!=HttpMethodType.POST)
			_httpMethod.setFollowRedirects(b);
	}

	public void addParameter(String paramName, String paramValue) {
		if (_methodType == HttpMethodType.POST) {
			((org.apache.commons.httpclient.methods.PostMethod) _httpMethod)
					.addParameter(paramName, paramValue);
		}
	}

	public boolean isHttp11() {
		return HttpVersion.HTTP_1_1 == _httpClient.getParams().getVersion();
	}

	public void setHttp11(boolean b) {
		if (b)
			_httpClient.getParams().setVersion(HttpVersion.HTTP_1_1);
		else
			_httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
	}

	public static void setProtocolSocketFactory(
			ProtocolSocketFactory socketFactory) {
		if (null != socketFactory)
			Protocol.registerProtocol("http", new Protocol("http",
					socketFactory, 80));
	}

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

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -