HttpClient4.3 使用经验(一) 简单使用

原创 2015年11月20日 12:01:09
package com.wp.nevel.base.utils;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

import com.wp.nevel.base.exception.ParserException;
import com.wp.nevel.base.service.impl.LogServiceHelp;

public class HttpClientUtils  {

	public static  Logger logger = Logger.getLogger( LogServiceHelp.class);
	
	private static HttpClient httpclient;

	static {
		httpclient = HttpClients.createDefault();
	}
	
	@Test
	public void test(){
		String url="http://www.shuchongw.com/files/article/html/23/23114/index.html";
		doGetHtmlContent2byte(url);
	}	
	
	
	/**
	 * 根据简单url获取网页数据,转换成byte [] 存储
	 * */
	public static byte[] doGetHtmlContent2byte(String url) {
		CloseableHttpResponse response = null;
		byte[] resultByte = {};
		try {
			HttpGet get = new HttpGet(url);
			System.out.println(url);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
			get.setConfig(requestConfig);
			try {
				response = (CloseableHttpResponse) HttpClientUtils.httpclient.execute(get);
			} catch (UnknownHostException e) {
				e.printStackTrace();	
				logger.info("链接主网失败");
			}
			int statusCode = response.getStatusLine().getStatusCode();
			System.out.println(statusCode);
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				resultByte = EntityUtils.toByteArray(entity);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		} finally {
			try {
				if(response!=null){
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultByte;
	}
	
	/**
	 * 根据复杂url获取网页数据,转换成byte [] 存储
	 * @throws ParserException 
	 * @throws IOException 
	 * @throws UnknownHostException 
	 * @throws ClientProtocolException 
	 * @throws SocketException 
	 * */
	public static byte [] doGetHtmlByParams2Byte(Map<String, String> params, String paramsEncoding, String url)  {
		if (params != null) {
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			for (Entry<String, String> entry : params.entrySet()) {
				formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			if (!formparams.isEmpty()) {
				String paramsStr = URLEncodedUtils.format(formparams, paramsEncoding!=null?paramsEncoding:"utf-8");
				url = url + "?" + paramsStr;
			}
		}
		return doGetHtmlContent2byte(url);
	}
	
	/**
	 * 根据复杂url获取网页数据,转换成String 存储
	 * @throws ParserException 
	 * @throws IOException 
	 * @throws UnknownHostException 
	 * @throws ClientProtocolException 
	 * @throws SocketException 
	 * */
	public static String doGetHtmlByParams2Text(Map<String, String> params, String paramsEncoding, String url,String htmlEncoding) throws  ClientProtocolException, UnknownHostException, SocketException{
		try {
			return getHtmlContentByText(doGetHtmlByParams2Byte(params,paramsEncoding,url),htmlEncoding!=null?htmlEncoding:"utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * 根据简单url获取网页数据,转换成String 存储
	 * @throws ParserException 
	 * @throws IOException 
	 * @throws UnknownHostException 
	 * @throws ClientProtocolException 
	 * @throws SocketException 
	 * */
	public static String doGetHtmlContentToString(String url, String encoding){
		try {
			return getHtmlContentByText(doGetHtmlContent2byte(url),encoding);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 根据简单url获取网页图片数据, 保存路径[saveImagePath]
	 * @throws ParserException 
	 * @throws IOException 
	 * @throws UnknownHostException 
	 * @throws ClientProtocolException 
	 * @throws SocketException 
	 * */
	public static  void getHtml2Image(String url,String saveImagPath){
		try {
			downloadData(doGetHtmlContent2byte(url),saveImagPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static  String getHtmlContentByText(byte [] htmlBytes, String encoding){
		try {
			return new String (htmlBytes,encoding!=null?encoding:"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return  "";
		}
	}
	
	
	/**
	 * 执行下载io
	 * 
	 * @param byte [] data 网页字节流,filename 存储地址和文件名 return
	 * */
	public static void downloadData(byte[] data, String filename) {
		try {
			DataOutputStream writer = new DataOutputStream(
					new FileOutputStream(new File(filename)));
			BufferedOutputStream out = new BufferedOutputStream(writer);
			out.write(data);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String readFile(String filename) throws IOException{
		String xmlContent =null;
		File file = new File(filename);
		BufferedReader reader =null;
		try {
			if(!file.exists()){
				 new RuntimeException("文件不存在");
				 return xmlContent;
			}
			StringBuilder buider = new StringBuilder();
			String readata ="";
			reader = new BufferedReader(new FileReader(file));
			while(true){
				readata = reader.readLine();
				if(readata==null){
					break;
				}
				buider.append(readata).append("\n");
			}
			xmlContent=buider.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(reader!=null)
				reader.close();
		}
		return xmlContent;
	}
	
	public static byte []  doGetByteByHttpclient2Url(HttpContext httpContext,CloseableHttpClient client,String url){
		byte [] resultBytes = null;
		try {
			HttpGet get = new HttpGet(url);
			CloseableHttpResponse response =client.execute(get, httpContext);
			int status = response.getStatusLine().getStatusCode();
			System.out.println("链接状态="+status);
			if(status!=200)
				return resultBytes;
			HttpEntity entity = response.getEntity();
			resultBytes = EntityUtils.toByteArray(entity);
		} catch (ClientProtocolException e) {
			throw new RuntimeException("失败连接地址"+url, e);
		} catch (IOException e) {
			throw new RuntimeException("失败连接地址"+url, e);
		}
		if(resultBytes==null){
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return resultBytes;
	}
	
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据

import java.security.KeyManagementException; import java.security.KeyStoreException; import java.sec...

HttpClient4.3的简单使用

最近在研究HttpClient的使用,简单写了这个测试类,来了解下HttpClient的使用 主要的流程是配置HttpClientBuilder的连接池管理,cookie,User-Agent,So...
  • jcbms
  • jcbms
  • 2017-03-30 20:40
  • 731

httpClient4.3使用

httpClient4.3.1的使用基本方法

HttpClient使用详解(4.3.X以上版本)

Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且...

httpClient4.3x 使用

网上关于httpClient4.3版本的介绍比较少,前期在用的时候碰到许多问题,每次都需要返工,这里做一个简单的小结,介绍一下httpClient4.3版本的使用。

Java使用HttpClient4.3发送GET/POST请求

使用HttpClient发送get、post请求 看到网上很多资料使用的版本都是3.x,目前官网最新版已经是4.5版本。使用4.3版本来模拟http请求。直接上code,可以直接用。 用到的jar...

Cordys BOP 4.3平台使用小经验—数据库连接管理、导出流程模型为XPDL

数据库连接管理小经验、导出流程模型为XPDL方法

经验分享:使用HttpClient做远程方法调用

近日完成一个系统,是从一个庞大的母系统中剥离出来的,因此不可避免地要调用母系统的API。我的开发和UT分两步走的,边界部分的代码使用变量devModel隔开(if ...else...)。具体是:  ...

简单的使用HttpClient访问网络

一开始HttpClient是由apache公司写的用于Java访问网络的一个编程工具包。 安卓出生以后,在安卓上也可以使用HttpClient来访问网络。 HttpClient在安...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)