代码是网上找的,然后自己写了一遍,感觉挺快的,现在贴出来
1 package httpclient; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 import org.apache.commons.io.IOUtils; 10 import org.apache.http.HttpEntity; 11 import org.apache.http.HttpResponse; 12 import org.apache.http.ParseException; 13 import org.apache.http.client.ClientProtocolException; 14 import org.apache.http.client.HttpClient; 15 import org.apache.http.client.methods.HttpPost; 16 import org.apache.http.client.params.ClientPNames; 17 import org.apache.http.conn.ClientConnectionManagerFactory; 18 import org.apache.http.entity.ByteArrayEntity; 19 import org.apache.http.entity.ContentType; 20 import org.apache.http.impl.client.DefaultHttpClient; 21 import org.apache.http.impl.conn.PoolingClientConnectionManager; 22 import org.apache.http.params.BasicHttpParams; 23 import org.apache.http.params.CoreConnectionPNames; 24 import org.apache.http.params.HttpParams; 25 import org.apache.http.util.EntityUtils; 26 27 /** 28 * 29 * <P>对httpclient-4.2.3.jar的简单使用</P> 30 * @ClassName: HttpClientTest 31 * @author 冯浩 2017年1月22日 下午2:48:43 32 * @see TODO 33 */ 34 public class HttpClientTest { 35 36 @org.junit.Test 37 public void Test() throws IOException{ 38 long start=System.currentTimeMillis(); 39 HttpParams params=new BasicHttpParams(); 40 Integer CONNECTION_TIMEOUT=2*1000;//请求超时- 41 Integer SO_TIMEOUT=2*1000;//等待数据超时 42 long CONN_MANAGER_TIMEOUT=500L;//连接不够时,等待超时时间 43 params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); 44 params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); 45 params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); 46 //提交之前进行连接测试 47 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true); 48 49 PoolingClientConnectionManager manager=new PoolingClientConnectionManager(); 50 manager.setMaxTotal(200);//连接池的最大连接数-pool的大小 51 manager.setDefaultMaxPerRoute(manager.getMaxTotal());//根据链接到的主机对池子大小进行细分 52 HttpClient client=new DefaultHttpClient(manager,params); 53 54 String url="http://******:8080/com.*************/****/*****"; 55 HttpPost post=new HttpPost(url); 56 HttpResponse response=null; 57 InputStream is=new FileInputStream(new File("D:/axtmp/20160815_120436-491.xml")); 58 59 HttpEntity en=new ByteArrayEntity(IOUtils.toByteArray(is),ContentType.TEXT_XML); 60 post.setEntity(en); 61 try { 62 response = client.execute(post); 63 } catch (ClientProtocolException e) { 64 e.printStackTrace(); 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } 68 HttpEntity entity = response.getEntity(); 69 try { 70 String result = EntityUtils.toString(entity, "utf-8"); 71 long end=System.currentTimeMillis(); 72 System.out.println(" result is "+result); 73 System.out.println(" process is "+(end-start)+" mills"); 74 } catch (ParseException e) { 75 e.printStackTrace(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 }