java网络编程____httpclient长连接请求长轮询



import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * @author Administrator
 * http长连接
 */
public class HttpClientLoop {
	/**
	 * 构造请求实体
	 * @param scehme
	 * @param host
	 * @param path
	 * @param nvps
	 * @return
	 * @throws Exception
	 */
	public static URI constructURI(final String scehme,final String host, final String path,List<NameValuePair>nvps) throws Exception{
		URI uri = new URIBuilder().setScheme(scehme)
				.setHost(host)
				.setPath(path).build();
				//.setParameters(nvps).build();
		return uri;
	}
	/**
	 * http长轮询 长连接
	 * @throws Exception 
	 */
	public static void httpLongConnLoop(final String scehme,final String host, final String path,List<NameValuePair>nvps) throws Exception{
		//http默认长连接策略
		ConnectionKeepAliveStrategy directStrategy = new ConnectionKeepAliveStrategy() {
			public long getKeepAliveDuration(HttpResponse response,HttpContext context) {
				//解析response信息
				HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
				while (it.hasNext()) {
					HeaderElement element = it.nextElement();
					String key = element.getName();
					String value = element.getValue();
					// 如果头部包含timeout信息,则使用
					if (value != null && key.equalsIgnoreCase("timeout")) {
						// 超时时间设置为服务器指定的值
						return Long.parseLong(value) * 1000;
					}
				}
				// 获取主机
				HttpHost target = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST);
				if (host.equalsIgnoreCase(target.getHostName())) {
					return 5 * 1000;
				} else {
					return 30 * 1000;
				}
			}
		};
		CloseableHttpClient client = HttpClients.custom().setKeepAliveStrategy(directStrategy).build();

		URI uri =constructURI(scehme, host, path, nvps);

		HttpPost post = new HttpPost(uri);

		try {
			CloseableHttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				System.out.println("解析结果集____:" + EntityUtils.toString(response.getEntity()));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//http://niuzhenxin.iteye.com/blog/2100100
	public static void main(String[] args) throws Exception {
		httpLongConnLoop("http", "niuzhenxin.iteye.com", "/blog/2100100", null);
	}
	
}

//

posted @ 2017-03-10 14:18  蜜獾互联网  阅读(53)  评论(0编辑  收藏  举报  来源