Skip to content

Commit 88556bb

Browse files
author
崔成龙
committed
为支持jdk1.6及以前版本,修改mutil-catch为单个抓取
1 parent f68f2db commit 88556bb

7 files changed

Lines changed: 81 additions & 43 deletions

File tree

src/main/java/com/tgb/ccl/http/common/SSLs.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
56
import java.io.IOException;
67
import java.security.KeyManagementException;
78
import java.security.KeyStore;
@@ -120,10 +121,20 @@ public SSLs customSSL(String keyStorePath, String keyStorepass) throws HttpProce
120121
try {
121122
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
122123
instream = new FileInputStream(new File(keyStorePath));
123-
trustStore.load(instream, keyStorepass.toCharArray());
124-
// 相信自己的CA和所有自签名的证书
125-
sc= SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .build();
126-
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | KeyManagementException e) {
124+
trustStore.load(instream, keyStorepass.toCharArray());
125+
// 相信自己的CA和所有自签名的证书
126+
sc= SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .build();
127+
} catch (KeyManagementException e) {
128+
throw new HttpProcessException(e);
129+
} catch (KeyStoreException e) {
130+
throw new HttpProcessException(e);
131+
} catch (FileNotFoundException e) {
132+
throw new HttpProcessException(e);
133+
} catch (NoSuchAlgorithmException e) {
134+
throw new HttpProcessException(e);
135+
} catch (CertificateException e) {
136+
throw new HttpProcessException(e);
137+
} catch (IOException e) {
127138
throw new HttpProcessException(e);
128139
}finally{
129140
try {

src/main/java/com/tgb/ccl/http/httpclient/HttpAsyncClientUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.apache.http.HttpEntity;
1515
import org.apache.http.HttpResponse;
1616
import org.apache.http.NameValuePair;
17-
import org.apache.http.ParseException;
1817
import org.apache.http.client.methods.CloseableHttpResponse;
1918
import org.apache.http.client.methods.HttpDelete;
2019
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
@@ -493,7 +492,9 @@ private static String fmt2String(HttpResponse resp, String encoding) throws Http
493492
EntityUtils.consume(entity);
494493
}
495494
}
496-
} catch (ParseException | IOException e) {
495+
} catch (UnsupportedOperationException e) {
496+
logger.error(e);
497+
} catch (IOException e) {
497498
logger.error(e);
498499
}
499500
return body;
@@ -511,7 +512,7 @@ private static OutputStream fmt2Stream(HttpResponse resp, OutputStream out) thro
511512
try {
512513
resp.getEntity().writeTo(out);
513514
EntityUtils.consume(resp.getEntity());
514-
} catch (ParseException | IOException e) {
515+
} catch (IOException e) {
515516
throw new HttpProcessException(e);
516517
}finally{
517518
close(resp);

src/main/java/com/tgb/ccl/http/httpclient/HttpClientUtil.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.http.HttpEntity;
1111
import org.apache.http.HttpResponse;
1212
import org.apache.http.NameValuePair;
13-
import org.apache.http.ParseException;
1413
import org.apache.http.client.HttpClient;
1514
import org.apache.http.client.methods.CloseableHttpResponse;
1615
import org.apache.http.client.methods.HttpDelete;
@@ -406,7 +405,7 @@ public static String fmt2String(HttpResponse resp, String encoding) throws HttpP
406405
logger.debug(body);
407406
}
408407
EntityUtils.consume(resp.getEntity());
409-
} catch (ParseException | IOException e) {
408+
} catch (IOException e) {
410409
throw new HttpProcessException(e);
411410
}finally{
412411
close(resp);
@@ -426,7 +425,7 @@ public static OutputStream fmt2Stream(HttpResponse resp, OutputStream out) throw
426425
try {
427426
resp.getEntity().writeTo(out);
428427
EntityUtils.consume(resp.getEntity());
429-
} catch (ParseException | IOException e) {
428+
} catch (IOException e) {
430429
throw new HttpProcessException(e);
431430
}finally{
432431
close(resp);

src/test/java/com/tgb/ccl/http/HttpAsyncClientTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,24 @@ public static void testMutilTask() throws HttpProcessException{
145145
// 设置header信息
146146
Header[] headers = HttpHeader.custom().userAgent("Mozilla/5.1").build();
147147
long start = System.currentTimeMillis();
148-
try {
149-
int pagecount = urls.length;
150-
ExecutorService executors = Executors.newFixedThreadPool(pagecount);
151-
CountDownLatch countDownLatch = new CountDownLatch(pagecount*10);
152-
handler.setCountDownLatch(countDownLatch);
153-
for(int i = 0; i< pagecount*10;i++){
154-
CloseableHttpAsyncClient client= HACB.custom().timeout(10000).proxy("127.0.0.1", 8087).ssl().build();
155-
FileOutputStream out = new FileOutputStream(new File("d://aaa//"+(i+1)+".png"));
156-
//启动线程抓取
157-
executors.execute(new GetRunnable(HttpConfig.custom().url(urls[i%pagecount]).headers(headers).handler(handler)));
158-
executors.execute(new GetRunnable(HttpConfig.custom().asynclient(client).url(imgurls[i%2]).headers(headers).out(out).handler(handler)));
159-
}
160-
countDownLatch.await();
161-
executors.shutdown();
162-
} catch (InterruptedException | FileNotFoundException e) {
163-
e.printStackTrace();
148+
try {
149+
int pagecount = urls.length;
150+
ExecutorService executors = Executors.newFixedThreadPool(pagecount);
151+
CountDownLatch countDownLatch = new CountDownLatch(pagecount*10);
152+
handler.setCountDownLatch(countDownLatch);
153+
for(int i = 0; i< pagecount*10;i++){
154+
CloseableHttpAsyncClient client= HACB.custom().timeout(10000).proxy("127.0.0.1", 8087).ssl().build();
155+
FileOutputStream out = new FileOutputStream(new File("d://aaa//"+(i+1)+".png"));
156+
//启动线程抓取
157+
executors.execute(new GetRunnable(HttpConfig.custom().url(urls[i%pagecount]).headers(headers).handler(handler)));
158+
executors.execute(new GetRunnable(HttpConfig.custom().asynclient(client).url(imgurls[i%2]).headers(headers).out(out).handler(handler)));
159+
}
160+
countDownLatch.await();
161+
executors.shutdown();
162+
} catch (FileNotFoundException e) {
163+
e.printStackTrace();
164+
} catch (InterruptedException e) {
165+
e.printStackTrace();
164166
} finally {
165167
System.out.println("线程" + Thread.currentThread().getName() + ", 所有线程已完成,开始进入下一步!");
166168
}

src/test/java/com/tgb/ccl/http/HttpClientTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,21 @@ public static void testMutilTask() throws HttpProcessException{
125125

126126
long start = System.currentTimeMillis();
127127
try {
128-
int pagecount = urls.length;
129-
ExecutorService executors = Executors.newFixedThreadPool(pagecount);
130-
CountDownLatch countDownLatch = new CountDownLatch(pagecount*10);
131-
for(int i = 0; i< pagecount*10;i++){
132-
FileOutputStream out = new FileOutputStream(new File("d://aaa//"+(i+1)+".png"));
133-
//启动线程抓取
134-
executors.execute(new GetRunnable(countDownLatch).setConfig(HttpConfig.custom().headers(headers).url(urls[i%pagecount])));
135-
executors.execute(new GetRunnable(countDownLatch).setConfig(HttpConfig.custom().client(client).headers(headers).url(imgurls[i%2]).out(out)));
136-
}
137-
countDownLatch.await();
138-
executors.shutdown();
139-
} catch (InterruptedException | FileNotFoundException e) {
140-
e.printStackTrace();
128+
int pagecount = urls.length;
129+
ExecutorService executors = Executors.newFixedThreadPool(pagecount);
130+
CountDownLatch countDownLatch = new CountDownLatch(pagecount*10);
131+
for(int i = 0; i< pagecount*10;i++){
132+
FileOutputStream out = new FileOutputStream(new File("d://aaa//"+(i+1)+".png"));
133+
//启动线程抓取
134+
executors.execute(new GetRunnable(countDownLatch).setConfig(HttpConfig.custom().headers(headers).url(urls[i%pagecount])));
135+
executors.execute(new GetRunnable(countDownLatch).setConfig(HttpConfig.custom().client(client).headers(headers).url(imgurls[i%2]).out(out)));
136+
}
137+
countDownLatch.await();
138+
executors.shutdown();
139+
} catch (FileNotFoundException e) {
140+
e.printStackTrace();
141+
} catch (InterruptedException e) {
142+
e.printStackTrace();
141143
} finally {
142144
System.out.println("线程" + Thread.currentThread().getName() + ", 所有线程已完成,开始进入下一步!");
143145
}

src/test/java/com/tgb/ccl/http/simpledemo/SimpleHttpAsyncClientDemo.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import java.io.InputStreamReader;
@@ -25,7 +26,6 @@
2526
import org.apache.http.HttpHost;
2627
import org.apache.http.HttpResponse;
2728
import org.apache.http.NameValuePair;
28-
import org.apache.http.ParseException;
2929
import org.apache.http.client.ClientProtocolException;
3030
import org.apache.http.client.entity.UrlEncodedFormEntity;
3131
import org.apache.http.client.methods.HttpPost;
@@ -74,7 +74,17 @@ public static SSLContext custom(String keyStorePath, String keyStorepass) {
7474
trustStore.load(instream, keyStorepass.toCharArray());
7575
// 相信自己的CA和所有自签名的证书
7676
sc = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
77-
} catch (KeyStoreException | NoSuchAlgorithmException| CertificateException | IOException | KeyManagementException e) {
77+
} catch (KeyManagementException e) {
78+
e.printStackTrace();
79+
} catch (KeyStoreException e) {
80+
e.printStackTrace();
81+
} catch (FileNotFoundException e) {
82+
e.printStackTrace();
83+
} catch (NoSuchAlgorithmException e) {
84+
e.printStackTrace();
85+
} catch (CertificateException e) {
86+
e.printStackTrace();
87+
} catch (IOException e) {
7888
e.printStackTrace();
7989
} finally {
8090
try {
@@ -219,7 +229,7 @@ public void completed(HttpResponse resp) {
219229
EntityUtils.consume(entity);
220230
}
221231
}
222-
} catch (ParseException | IOException e) {
232+
} catch (IOException e) {
223233
e.printStackTrace();
224234
}
225235
handler.completed(body);

src/test/java/com/tgb/ccl/http/simpledemo/SimpleHttpClientDemo.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
56
import java.io.IOException;
67
import java.security.KeyManagementException;
78
import java.security.KeyStore;
@@ -67,8 +68,20 @@ public static SSLContext custom(String keyStorePath, String keyStorepass) {
6768
trustStore.load(instream, keyStorepass.toCharArray());
6869
// 相信自己的CA和所有自签名的证书
6970
sc = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
70-
} catch (KeyStoreException | NoSuchAlgorithmException| CertificateException | IOException | KeyManagementException e) {
71+
} catch (KeyManagementException e) {
7172
e.printStackTrace();
73+
} catch (KeyStoreException e) {
74+
e.printStackTrace();
75+
} catch (FileNotFoundException e) {
76+
e.printStackTrace();
77+
} catch (NoSuchAlgorithmException e) {
78+
e.printStackTrace();
79+
} catch (CertificateException e) {
80+
e.printStackTrace();
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
// } catch (KeyStoreException | NoSuchAlgorithmException| CertificateException | IOException | KeyManagementException e) {
84+
// e.printStackTrace();
7285
} finally {
7386
try {
7487
instream.close();

0 commit comments

Comments
 (0)