HttpClient

 

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientTest_02_down {

	public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException, InterruptedException {
		File imageFiles = new File("d:/upload/images.txt");
//		BufferedReader reader = new BufferedReader(new FileReader(imageFiles));
		InputStreamReader isr = new InputStreamReader(new FileInputStream(imageFiles), "UTF-8");
		BufferedReader reader = new BufferedReader(isr);
		String lineStr = "";
		while((lineStr = reader.readLine()) != null){
			Thread.currentThread().sleep(100);
			System.out.println(lineStr);
			if(lineStr != null && !"".equals(lineStr)){
				String productCode = lineStr.substring(0,lineStr.indexOf(";"));
				String productName = lineStr.substring(lineStr.indexOf(";") + 1,lineStr.lastIndexOf(";"));
//				String fileName = lineStr.substring(lineStr.lastIndexOf("/")+1);
				String suffix = lineStr.substring(lineStr.lastIndexOf("."));
				String url = lineStr.substring(lineStr.lastIndexOf(";") + 1);
				download3(url,"d:/upload/images/" + productCode + "---" + productName + suffix);
			}
		}
		reader.close();
		
//		String lineStr = "MP02000139;平安燃氣寶;https://elis-ecocdn.pingan.com.cn/cmsinfo/upload/product//MP02000139/674490280-_-_-1495863292411.jpg";
//		String suffix = lineStr.substring(lineStr.lastIndexOf("."));
//		String url = lineStr.substring(lineStr.lastIndexOf(";") + 1);
//		System.out.println(url);
	}
	
	public static void download3(String URL_STR, String localFileName) {
        FileOutputStream out = null;
        InputStream in = null;
//        String URL_STR = "https://elis-ecocdn.pingan.com.cn/cmsinfo/upload/product//3046/caifujinsheng0-_-_-1482827584912.jpg";
        
        try{
            URL url = new URL(URL_STR);
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            
            // true -- will setting parameters
            httpURLConnection.setDoOutput(true);
            // true--will allow read in from
            httpURLConnection.setDoInput(true);
            // will not use caches
            httpURLConnection.setUseCaches(false);
            // setting serialized
            httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
            httpURLConnection.setRequestProperty("Content-Length", "44746");
            // default is GET                        
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charsert", "UTF-8");
            // 1 min
            httpURLConnection.setConnectTimeout(60000);
            // 1 min
            httpURLConnection.setReadTimeout(60000);

//            httpURLConnection.addRequestProperty("userName", userName);
//            httpURLConnection.addRequestProperty("passwd", passwd);
//            httpURLConnection.addRequestProperty("fileName", remoteFileName);

            // connect to server (tcp)
            httpURLConnection.connect();

            in = httpURLConnection.getInputStream();// send request to
                                                                // server
            File file = new File(localFileName);
            if(!file.exists()){
                file.createNewFile();
            }

            out = new FileOutputStream(file);  
            byte[] buffer = new byte[4096];
            int readLength = 0;
            while ((readLength=in.read(buffer)) > 0) {
                byte[] bytes = new byte[readLength];
                System.arraycopy(buffer, 0, bytes, 0, readLength);
                out.write(bytes);
            }
            
            out.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
		
//	public static void downLoad(String remoteFileName, String localFileName) {
//        DefaultHttpClient httpClient = new DefaultHttpClient();
//        OutputStream out = null;
//        InputStream in = null;
//        
////        String URL_STR = "http://www.baidu.com";
//        String URL_STR = "https://elis-ecocdn.pingan.com.cn/cmsinfo/upload/product//3046/caifujinsheng0-_-_-1482827584912.jpg";
//        try {
//            HttpGet httpGet = new HttpGet(URL_STR);
//
////            httpGet.addHeader("userName", userName);
////            httpGet.addHeader("passwd", passwd);
//            httpGet.addHeader("fileName", remoteFileName);
//
//            HttpResponse httpResponse = httpClient.execute(httpGet);
//            HttpEntity entity = httpResponse.getEntity();
//            in = entity.getContent();
//
//            long length = entity.getContentLength();
//            if (length <= 0) {
//                System.out.println("下載文件不存在!");
//                return;
//            }
//
//            System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));
//
//            File file = new File(localFileName);
//            if(!file.exists()){
//                file.createNewFile();
//            }
//            
//            out = new FileOutputStream(file);  
//            byte[] buffer = new byte[4096];
//            int readLength = 0;
//            while ((readLength=in.read(buffer)) > 0) {
//                byte[] bytes = new byte[readLength];
//                System.arraycopy(buffer, 0, bytes, 0, readLength);
//                out.write(bytes);
//            }
//            
//            out.flush();
//            
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally{
//            try {
//                if(in != null){
//                    in.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            
//            try {
//                if(out != null){
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//		
	
	public static void p(Object ooo){
		System.out.println( ooo + "" );
	}

}
package com.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class HttpClientTest_01_fundamentals {

	public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException {
//		test_base();
//		test_url();
//		test_response();
//		test_consume_httpentity();
//		test_produce_httpentity();
//		test_html_form();
		test_html_form_302();
	}

	/**
	 * Request execution
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	@Test
	public static void test_request_execution() throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpget = new HttpGet("https://test3-elis-cms.pa18.com:41319/smp-cms-dmz/");
//		HttpGet httpget = new HttpGet("https://elis-ecocdn.pingan.com.cn/cmsinfo/upload/product//3046/caifujinsheng0-_-_-1482827584912.jpg");
//		HttpGet httpget = new HttpGet("http://sqms-dpm.paic.com.cn/dpm/login#");
		CloseableHttpResponse response = httpclient.execute(httpget);
		try{
			Header[] headers = response.getAllHeaders();
			for (Header header : headers) {
				p(header.getName() + ":" + header.getValue());
			}
		} finally {
			response.close();
		}
	}

	/**
	 * HTTP request
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@Test
	public static void test_http_request() throws ClientProtocolException, IOException, URISyntaxException {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		URI uri = new URIBuilder().setScheme("http")
								  .setHost("localhost:8080")
								  .setPath("/smp-cms-web/index.html")
//								  .setParameter("wd", "search")
//								  .setParameter("btnG", "Google Search")
//								  .setParameter("aq", "f")
//								  .setParameter("oq", "")
								  .build();
		HttpGet httpget = new HttpGet(uri);
		p(httpget.getURI());
	}
	
	/**
	 * HTTP response
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@Test
	public static void test_http_response() throws ClientProtocolException, IOException, URISyntaxException {
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		URI uri = new URIBuilder().setScheme("http")
								  .setHost("localhost:8080")
								  .setPath("/smp-cms-web/index.html")
//								  .setParameter("wd", "search")
//								  .setParameter("btnG", "Google Search")
//								  .setParameter("aq", "f")
//								  .setParameter("oq", "")
								  .build();
		HttpGet httpget = new HttpGet(uri);
		p(httpget.getURI());
		
		p("--------------------------------response properties-------------------------------");
		CloseableHttpResponse response = httpclient.execute(httpget);
		
		p(response.getProtocolVersion());
		p(response.getStatusLine().getStatusCode());
		p(response.getStatusLine().getReasonPhrase());
		p(response.getStatusLine().toString());
		
		p("------------------------------response headers---------------------------------");
		Header[] headers = response.getAllHeaders();
		for (Header header : headers) {
			p(header.getName() + ":" + header.getValue());
		}
		
		p("--------------------------------speciafy headers-------------------------------");
		response.addHeader("zjj","c1=a; path=/; domain=localhost");
		response.addHeader("zjj","aaa2");
		response.addHeader("zjj","c2=b; path=\"/\", c3=c; domain=\"localhost\"");
		p(response.getFirstHeader("zjj"));
		p(response.getLastHeader("zjj"));
		
		p("--------------------------------HeaderElementIterator-------------------------------");
		HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("zjj"));
		
		while (it.hasNext()) {
			HeaderElement elem = it.nextElement();
			System.out.println(elem.getName() + " = " + elem.getValue());
			NameValuePair[] params = elem.getParameters();
			for (int i = 0; i < params.length; i++) {
				System.out.println(" " + params[i]);
			}
		}
		
		p("-------------------------------HttpEntity--------------------------------");
		HttpEntity entity = response.getEntity();
		p(entity);
		p(entity.getContentType());
		p(entity.getContentLength());
		p(entity.getContentEncoding());
		p(entity.getContent());
		
	}
	
	/**
	 * Ensuring release of low level resources
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@Test
	public static void test_consume_httpentity() throws ClientProtocolException, IOException, URISyntaxException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpget = new HttpGet("http://elis-smp-cms-stg3.paic.com.cn:41318/smp-cms-web/");
		CloseableHttpResponse response = httpclient.execute(httpget);	//和HttpResponse沒有關係?
		HttpEntity entity = response.getEntity();
		try{
			if(entity != null){
				p(entity);

				/* 此時entity只能被讀取一次,好比下面的兩種讀取方式,第二種就會讀取失敗:Attempted read from closed stream.
				 * 解決辦法是:
				 * 屢次讀取entity時,必需要存在在內存或者硬盤中,可使用BufferedHttpEntity包裝
				 * 以下:
				 * */
				entity = new BufferedHttpEntity(entity);
				
				//方式1:使用InputStream打印內容--注意關閉流
				InputStream instream = entity.getContent();
				byte[] bytes = new byte[instream.available()];
				try {
					while(instream.read(bytes) > -1){
						System.out.println(new String(bytes,"utf-8"));
					}
				} finally {
					instream.close();
				}
				
				p("------------------------------------------------------------------------------");
				/*方式2:
				 * Consuming entity content
				 * 使用entity打印出response返回的內容(不包裝直接讀取會報錯)
				 * */
				long len = entity.getContentLength();
				if (len != -1 && len < 20480) {
					p(EntityUtils.toString(entity));
				}
				//注:instream.available()獲取的長度和entity.getContentLength()是一致的,固然後者簡單
			}
		} finally {
			response.close();
		}
	}
	/**
	 * Producing entity content
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@Test
	public static void test_produce_httpentity() throws ClientProtocolException, IOException, URISyntaxException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		File file = new File("somefile.txt");
		FileEntity entity = new FileEntity(file,ContentType.create("text/plain", "UTF-8"));
		HttpPost httppost = new HttpPost("https://test3-elis-cms.pa18.com:41319/smp-cms-dmz/");
		httppost.setEntity(entity);
		CloseableHttpResponse response = httpclient.execute(httppost);
		try{
		} finally {
			response.close();
		}
	}
	
	/**
	 * HTML forms
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@Test
	public static void test_html_form() throws ClientProtocolException, IOException, URISyntaxException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		
		formparams.add(new BasicNameValuePair("j_username", "yangli1"));
		formparams.add(new BasicNameValuePair("pwd", "aaaaa555"));
		
		UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
//		HttpPost httppost = new HttpPost("http://localhost:8080/smp-cms-web/login.do");
		HttpPost httppost = new HttpPost("http://elis-smp-cms-stg3.paic.com.cn:41318/smp-cms-web/");
		httppost.setEntity(entityParam);
		
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		long len = entity.getContentLength();
		if (len != -1 && len < 20480) {
			p(EntityUtils.toString(entity));
		}
	}
	
	@Test
	public static void test_html_form_302() throws ClientProtocolException, IOException, URISyntaxException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("j_username", "yangli1"));
		formparams.add(new BasicNameValuePair("pwd", "aaaaa555"));
		UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
		HttpPost httppost = new HttpPost("http://localhost:8080/smp-cms-web/login.do");
		httppost.setEntity(entityParam);
		
		HttpResponse response = httpclient.execute(httppost);
		p(response.getStatusLine());
		HttpEntity entity = response.getEntity();
		if(entity != null){
			p(entity);
			long len = entity.getContentLength();
			if (len != -1 && len < 20480) {
				p(EntityUtils.toString(entity));
			} else {
			}
		}
		p("-------------------------------response : 302--------------------------------");
		
		httppost.abort();	//釋放post請求  
		if (response.getStatusLine().getStatusCode() == 302) {
			String locationUrl = response.getLastHeader("Location").getValue();
			HttpGet httpget = new HttpGet(locationUrl);// 跳轉到重定向的url
			HttpResponse resp = httpclient.execute(httpget);
			HttpEntity enti = resp.getEntity();
			if(enti != null){
				p(enti);
				long len = enti.getContentLength();
				if (len != -1 && len < 20480) {
					p(EntityUtils.toString(enti));
				} else {
					
				}
			}
			
		}
		
	}
	
	
	public static void p(Object ooo){
		System.out.println( ooo + "" );
	}

}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息