Skip to content

Commit 713a430

Browse files
committedJul 22, 2016
修复不能准确抛出网络异常的bug
1 parent 9d8f535 commit 713a430

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed
 

‎apache-client/src/main/java/com/litesuits/http/impl/apache/ApacheClient.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import com.litesuits.http.HttpConfig;
66
import com.litesuits.http.data.*;
77
import com.litesuits.http.data.HttpStatus;
8-
import com.litesuits.http.exception.HttpClientException;
9-
import com.litesuits.http.exception.HttpServerException;
10-
import com.litesuits.http.exception.ServerException;
8+
import com.litesuits.http.exception.*;
119
import com.litesuits.http.listener.StatisticsListener;
1210
import com.litesuits.http.log.HttpLog;
1311
import com.litesuits.http.parser.DataParser;
@@ -239,14 +237,15 @@ public <T> void connect(AbstractRequest<T> request, InternalResponse response) t
239237
}
240238
response.setHeaders(hs);
241239
}
242-
// 成功
243-
entity = ares.getEntity();
244240

245241
// is cancelled ?
246242
if (request.isCancelledOrInterrupted()) {
247243
return;
248244
}
249245

246+
// 成功
247+
entity = ares.getEntity();
248+
250249
// data body
251250
if (status.getStatusCode() <= 299 || status.getStatusCode() == 600) {
252251
if (entity != null) {
@@ -267,6 +266,8 @@ public <T> void connect(AbstractRequest<T> request, InternalResponse response) t
267266
}
268267
response.setReadedLength(parser.getReadedLength());
269268
}
269+
}else{
270+
throw new HttpNetException(NetException.NetworkUnreachable);
270271
}
271272
} else if (status.getStatusCode() <= 399) {
272273
// redirect

‎litehttp/src/main/java/com/litesuits/http/LiteHttp.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ public <T> Response<T> execute(AbstractRequest<T> request) {
246246
if (HttpLog.isPrint) {
247247
Thread t = Thread.currentThread();
248248
HttpLog.i(TAG,
249-
"lite http request: " + request.createFullUri()
250-
+ " , tag: " + request.getTag()
251-
+ " , method: " + request.getMethod()
252-
+ " , cache mode: " + request.getCacheMode()
253-
+ " , thread ID: " + t.getId()
254-
+ " , thread name: " + t.getName());
249+
"lite http request: " + request.createFullUri()
250+
+ " , tag: " + request.getTag()
251+
+ " , method: " + request.getMethod()
252+
+ " , cache mode: " + request.getCacheMode()
253+
+ " , thread ID: " + t.getId()
254+
+ " , thread name: " + t.getName());
255255
}
256256
if (globalListener != null) {
257257
globalListener.notifyCallStart(request);
@@ -447,10 +447,12 @@ public <T> void connectWithRetries(AbstractRequest<T> request, InternalResponse
447447
}
448448
// connect and parse data
449449
config.httpClient.connect(request, response);
450-
} catch (IOException e) {
451-
cause = e;
450+
} catch (HttpServerException | HttpNetException e) {
451+
throw e;
452452
} catch (SecurityException e) {
453453
throw new HttpClientException(e, ClientException.PermissionDenied);
454+
} catch (IOException e) {
455+
cause = e;
454456
} catch (Exception e) {
455457
throw new HttpClientException(e);
456458
}

‎litehttp/src/main/java/com/litesuits/http/impl/huc/HttpUrlClient.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import com.litesuits.http.data.Consts;
88
import com.litesuits.http.data.HttpStatus;
99
import com.litesuits.http.data.NameValuePair;
10-
import com.litesuits.http.exception.HttpNetException;
11-
import com.litesuits.http.exception.HttpServerException;
12-
import com.litesuits.http.exception.NetException;
13-
import com.litesuits.http.exception.ServerException;
10+
import com.litesuits.http.exception.*;
1411
import com.litesuits.http.listener.StatisticsListener;
1512
import com.litesuits.http.log.HttpLog;
1613
import com.litesuits.http.parser.DataParser;
@@ -24,10 +21,7 @@
2421
import java.io.InputStream;
2522
import java.io.InterruptedIOException;
2623
import java.io.OutputStream;
27-
import java.net.HttpURLConnection;
28-
import java.net.SocketTimeoutException;
29-
import java.net.URI;
30-
import java.net.URL;
24+
import java.net.*;
3125
import java.security.cert.CertificateException;
3226
import java.security.cert.X509Certificate;
3327
import java.util.ArrayList;
@@ -69,7 +63,7 @@ public void config(HttpConfig config) {
6963

7064
@Override
7165
public <T> void connect(AbstractRequest<T> request, InternalResponse response)
72-
throws Exception {
66+
throws HttpClientException, IOException, URISyntaxException, HttpNetException, HttpServerException {
7367
InputStream inputStream = null;
7468
int maxRedirectTimes = request.getMaxRedirectTimes();
7569
StatisticsListener statistic = response.getStatistics();

0 commit comments

Comments
 (0)
Please sign in to comment.