123456789101112131415161718192021222324252627282930313233 |
- package com.muzhi.zgd.handler;
- import com.muzhi.zgd.exception.HttpResponseException;
- import com.muzhi.zgd.http.HttpIOUtils;
- import com.muzhi.zgd.http.HttpStatus;
- import java.io.IOException;
- import java.net.HttpURLConnection;
- public class PlainTextResponseHandler implements ResponseHandler<String> {
- @Override
- public void preHandle(HttpURLConnection httpConn) {
- httpConn.setConnectTimeout(5000);
- httpConn.setReadTimeout(10000);
- }
- @Override
- public String handleResponse(HttpURLConnection httpConn, String charset) throws IOException {
- int status = httpConn.getResponseCode();
- if (status >= HttpURLConnection.HTTP_OK && status < HttpURLConnection.HTTP_MULT_CHOICE) {
- return HttpIOUtils.toInputText(httpConn.getInputStream(), charset);
- } else {
- String result = HttpIOUtils.toInputText(httpConn.getErrorStream(), charset);
- if(result != null && result.length() > 0) {
- throw new HttpResponseException(status, result.toString());
- }
- throw new HttpResponseException(status, HttpStatus.getStatusText(status));
- }
- }
- }
|