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

org.frameworkset.spi.remote.http.HttpRequestUtil Maven / Gradle / Ivy

/**
 * 
 */
package org.frameworkset.spi.remote.http;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
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.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @author yinbp
 *
 * @Date:2016-11-20 11:39:59
 */
public class HttpRequestUtil {

	public static final String UTF_8 = "UTF-8";
	// public static final String DESC = "descend";
	// public static final String ASC = "ascend";

	private final static int TIMEOUT_CONNECTION = 20000;
	private final static int TIMEOUT_SOCKET = 20000;
	private final static int RETRY_TIME = 3;

	private static HttpClient getHttpClient() throws Exception {
		return ClientConfiguration.getHttpClient();
	}

	private static String getCookie() {
		// if(appCookie == null || appCookie == "") {
		// appCookie = appContext.getProperty("cookie");
		// }
		// return appCookie;
		return null;
	}

	private static String getUserAgent() {
		// if(appUserAgent == null || appUserAgent == "") {
		// StringBuilder ua = new StringBuilder("OSChina.NET");
		// ua.append('/'+appContext.getPackageInfo().versionName+'_'+appContext.getPackageInfo().versionCode);//App版本
		// ua.append("/Android");//手机系统平台
		// ua.append("/"+android.os.Build.VERSION.RELEASE);//手机系统版本
		// ua.append("/"+android.os.Build.MODEL); //手机型号
		// ua.append("/"+appContext.getAppId());//客户端唯一标识
		// appUserAgent = ua.toString();
		// }
		// return appUserAgent;
		return null;
	}

	private static HttpGet getHttpGet(String url, String cookie, String userAgent) {

		HttpGet httpget = new HttpGet(url);
		// Request configuration can be overridden at the request level.
		// They will take precedence over the one set at the client level.
		RequestConfig requestConfig = RequestConfig.copy(ClientConfiguration.getDefaultRequestConfig())
				.setSocketTimeout(5000).setConnectTimeout(TIMEOUT_CONNECTION)
				.setConnectionRequestTimeout(TIMEOUT_CONNECTION)
				// .setProxy(new HttpHost("myotherproxy", 8080))
				.build();
		httpget.setConfig(requestConfig);
		httpget.addHeader("Host", "www.bbossgroups.com");
		httpget.addHeader("Connection", "Keep-Alive");
		if (cookie != null)
			httpget.addHeader("Cookie", cookie);
		if (userAgent != null)
			httpget.addHeader("User-Agent", userAgent);
		return httpget;
	}

	private static HttpPost getHttpPost(String url, String cookie, String userAgent) {
		HttpPost httpPost = new HttpPost(url);
		RequestConfig requestConfig = RequestConfig.copy(ClientConfiguration.getDefaultRequestConfig())
				.setSocketTimeout(5000).setConnectTimeout(TIMEOUT_CONNECTION)
				.setConnectionRequestTimeout(TIMEOUT_CONNECTION)
				// .setProxy(new HttpHost("myotherproxy", 8080))
				.build();
		httpPost.setConfig(requestConfig);
		httpPost.addHeader("Host", "www.bbossgroups.com");
		httpPost.addHeader("Connection", "Keep-Alive");
		if (cookie != null)
			httpPost.addHeader("Cookie", cookie);
		if (userAgent != null)
			httpPost.addHeader("User-Agent", userAgent);

		return httpPost;
	}

	public static String httpGetforString(String url) throws Exception {
		return httpGetforString(url, (String) null, (String) null);
	}

	/**
	 * get请求URL
	 * 
	 * @param url
	 * @throws AppException
	 */
	public static String httpGetforString(String url, String cookie, String userAgent) throws Exception {
		// String cookie = getCookie();
		// String userAgent = getUserAgent();

		HttpClient httpClient = null;
		HttpGet httpGet = null;

		String responseBody = "";
		int time = 0;
		do {
			try {
				httpClient = getHttpClient();
				httpGet = getHttpGet(url, cookie, userAgent);

				// Create a custom response handler
				ResponseHandler responseHandler = new ResponseHandler() {

					@Override
					public String handleResponse(final HttpResponse response)
							throws ClientProtocolException, IOException {
						int status = response.getStatusLine().getStatusCode();
						if (status >= 200 && status < 300) {
							HttpEntity entity = response.getEntity();
							return entity != null ? EntityUtils.toString(entity) : null;
						} else {
							throw new ClientProtocolException("Unexpected response status: " + status);
						}
					}

				};
				responseBody = httpClient.execute(httpGet, responseHandler);
				break;
			} catch (ClientProtocolException e) {
				throw new HttpRuntimeException("请求异常:", e);
			} catch (IOException e) {
				time++;
				if (time < RETRY_TIME) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
					}
					continue;
				}
				// 发生致命的异常,可能是协议不对或者返回的内容有问题
				throw new HttpRuntimeException("请求异常:", e);
			} catch (Exception e) {
				time++;
				if (time < RETRY_TIME) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
					}
					continue;
				}
				// 发生网络异常
				e.printStackTrace();
				throw new HttpRuntimeException("请求异常:", e);
			} finally {
				// 释放连接
				httpGet.releaseConnection();
				httpClient = null;
			}
		} while (time < RETRY_TIME);

		return responseBody;
		// //responseBody = responseBody.replaceAll("\\p{Cntrl}", "\r\n");
		// if(responseBody.contains("result") &&
		// responseBody.contains("errorCode") &&
		// appContext.containsProperty("user.uid")){
		// try {
		// Result res = Result.parse(new
		// ByteArrayInputStream(responseBody.getBytes()));
		// if(res.getErrorCode() == 0){
		// appContext.Logout();
		// appContext.getUnLoginHandler().sendEmptyMessage(1);
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		// return new ByteArrayInputStream(responseBody.getBytes());
	}

	/**
	 * 公用post方法
	 * 
	 * @param url
	 * @param params
	 * @param files
	 * @throws AppException
	 */
	public static String httpPostforString(String url, Map params, Map files)
			throws Exception {
		return httpPostforString(url, (String) null, (String) null, params, files);
	}

	/**
	 * 公用post方法
	 * 
	 * @param url
	 * @param params
	 * @param files
	 * @throws AppException
	 */
	public static String httpPostforString(String url, Map params) throws Exception {
		return httpPostforString(url, (String) null, (String) null, params, (Map) null);
	}

	/**
	 * 公用post方法
	 * 
	 * @param url
	 * @param params
	 * @param files
	 * @throws AppException
	 */
	public static String httpPostforString(String url) throws Exception {
		return httpPostforString(url, (String) null, (String) null, (Map) null,
				(Map) null);
	}
	public static String httpPostforString(String url, String cookie, String userAgent,
			Map files) throws Exception {
		return httpPostforString(url, cookie, userAgent, null,
				files);
	}
	/**
	 * 公用post方法
	 * 
	 * @param url
	 * @param params
	 * @param files
	 * @throws AppException
	 */
	public static String httpPostforString(String url, String cookie, String userAgent, Map params,
			Map files) throws Exception {
		// System.out.println("post_url==> "+url);
		// String cookie = getCookie(appContext);
		// String userAgent = getUserAgent(appContext);

		HttpClient httpClient = null;
		HttpPost httpPost = null;
		MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
//				
//                .addPart("bin", bin)
//                .addPart("comment", comment)
//                .build();
//				 FileBody bin = new FileBody(new File(args[0]));
//        StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
		HttpEntity httpEntity = null;
		List paramPair = null;
		if (files != null) {
			// post表单参数处理
			int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size());
			 
			int i = 0;
			boolean hasdata = false;
			 
			if (params != null) {
				Iterator> it = params.entrySet().iterator();
				while (it.hasNext()) {
					Entry entry = it.next();
					multipartEntityBuilder.addTextBody(entry.getKey(), String.valueOf(entry.getValue()),ClientConfiguration.TEXT_PLAIN_UTF_8);
					hasdata = true;
				}
			}
			if (files != null) {
				Iterator> it = files.entrySet().iterator();
				while (it.hasNext()) {
					Entry entry = it.next();
					 
//						parts[i++] = new FilePart(entry.getKey(), entry.getValue());
						File f = new File(String.valueOf(entry.getValue()));
						if(f.exists())
						{
							 FileBody file = new FileBody(f);
							multipartEntityBuilder.addPart(entry.getKey(), file);
							hasdata = true;
						}
						else
						{
							
						}
					 
					// System.out.println("post_key_file==> "+file);
				}
			}
			if(hasdata)
				httpEntity = multipartEntityBuilder.build();
		} else if (params != null && params.size() > 0) {
			paramPair = new ArrayList();
			Iterator> it = params.entrySet().iterator();
			NameValuePair paramPair_ = null;
			for (int i = 0; it.hasNext(); i++) {
				Entry entry = it.next();
				paramPair_ = new BasicNameValuePair(entry.getKey(),String.valueOf(entry.getValue()));			 
				paramPair.add(paramPair_);
			}
		}

		String responseBody = "";
		int time = 0;
		do {
			try {
				httpClient = getHttpClient();
				httpPost = getHttpPost(url, cookie, userAgent);
			


				
				if (httpEntity != null ) {
					httpPost.setEntity(httpEntity);
				} else if(paramPair != null && paramPair.size() > 0){
					UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramPair, Consts.UTF_8);

					httpPost.setEntity(entity);

				}
				// Create a custom response handler
				ResponseHandler responseHandler = new ResponseHandler() {

					@Override
					public String handleResponse(final HttpResponse response)
							throws ClientProtocolException, IOException {
						int status = response.getStatusLine().getStatusCode();
						if (status >= 200 && status < 300) {
							HttpEntity entity = response.getEntity();
							return entity != null ? EntityUtils.toString(entity) : null;
						} else {
							throw new ClientProtocolException("Unexpected response status: " + status);
						}
					}

				};
				responseBody = httpClient.execute(httpPost, responseHandler);				
				break;
			}
			catch(ClientProtocolException e)
			{
				throw new HttpRuntimeException("请求异常:", e);
			}
			catch (HttpException e) {
				time++;
				if (time < RETRY_TIME) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
					}
					continue;
				}
				// 发生致命的异常,可能是协议不对或者返回的内容有问题
				throw new HttpRuntimeException("请求异常:", e);
			} catch (IOException e) {
				time++;
				if (time < RETRY_TIME) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
					}
					continue;
				}
				// 发生网络异常
				throw new HttpRuntimeException("请求异常:", e);
			} finally {
				// 释放连接
				//httpPost.releaseConnection();
				httpClient = null;
			}
		} while (time < RETRY_TIME);
		return responseBody;
		
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy