| @@ -4,7 +4,6 @@ import okhttp3.MediaType; | |||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | import okhttp3.Request; | ||||
| import okhttp3.RequestBody; | import okhttp3.RequestBody; | ||||
| import org.apache.commons.codec.Charsets; | |||||
| import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
| import org.apache.http.*; | import org.apache.http.*; | ||||
| import org.apache.http.client.entity.UrlEncodedFormEntity; | import org.apache.http.client.entity.UrlEncodedFormEntity; | ||||
| @@ -19,6 +18,8 @@ import org.apache.http.message.BasicNameValuePair; | |||||
| import org.apache.http.protocol.HTTP; | import org.apache.http.protocol.HTTP; | ||||
| import org.apache.http.ssl.SSLContexts; | import org.apache.http.ssl.SSLContexts; | ||||
| import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import javax.net.ssl.HttpsURLConnection; | import javax.net.ssl.HttpsURLConnection; | ||||
| import javax.net.ssl.KeyManager; | import javax.net.ssl.KeyManager; | ||||
| @@ -34,7 +35,6 @@ import java.util.ArrayList; | |||||
| import java.util.Iterator; | import java.util.Iterator; | ||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.Map; | import java.util.Map; | ||||
| import java.util.logging.Logger; | |||||
| /** | /** | ||||
| @@ -44,7 +44,7 @@ import java.util.logging.Logger; | |||||
| */ | */ | ||||
| public class HttpUtil { | public class HttpUtil { | ||||
| private static Logger logger = Logger.getLogger(String.valueOf(HttpUtil.class)); | |||||
| private static final Logger logger = LoggerFactory.getLogger(ETCPUtil.class); | |||||
| private static final MediaType CONTENT_TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded"); | private static final MediaType CONTENT_TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded"); | ||||
| private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"; | private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"; | ||||
| @@ -70,7 +70,8 @@ public class HttpUtil { | |||||
| } | } | ||||
| } | } | ||||
| catch (IOException e) { | catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | |||||
| throw new RuntimeException(e); | |||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -124,13 +125,12 @@ public class HttpUtil { | |||||
| return sb.toString(); | return sb.toString(); | ||||
| } | } | ||||
| else{ // | else{ // | ||||
| System.out.println("状态码:" + code); | |||||
| logger.info("状态码:" + code); | |||||
| client.close(); | client.close(); | ||||
| } | } | ||||
| } | } | ||||
| catch(Exception e){ | catch(Exception e){ | ||||
| e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | |||||
| return null; | return null; | ||||
| } | } | ||||
| @@ -172,13 +172,13 @@ public class HttpUtil { | |||||
| try { | try { | ||||
| response.close(); | response.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | |||||
| } | } | ||||
| } | } | ||||
| try { | try { | ||||
| httpclient.close(); | httpclient.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | |||||
| } | } | ||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -196,6 +196,7 @@ public class HttpUtil { | |||||
| .url(url).post(body).build(); | .url(url).post(body).build(); | ||||
| return exec(request); | return exec(request); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| logger.error(e.getMessage()); | |||||
| return null; | return null; | ||||
| } | } | ||||
| } | } | ||||
| @@ -207,6 +208,7 @@ public class HttpUtil { | |||||
| throw new RuntimeException("Unexpected code " + response); | throw new RuntimeException("Unexpected code " + response); | ||||
| return response.body().string(); | return response.body().string(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| logger.error(e.getMessage()); | |||||
| throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
| } | } | ||||
| } | } | ||||
| @@ -252,6 +254,7 @@ public class HttpUtil { | |||||
| } | } | ||||
| return sb.toString(); | return sb.toString(); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| logger.error(e.getMessage()); | |||||
| throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
| } finally { | } finally { | ||||
| IOUtils.closeQuietly(out); | IOUtils.closeQuietly(out); | ||||
| @@ -289,17 +292,17 @@ public class HttpUtil { | |||||
| HttpPost httpPost = new HttpPost(url); | HttpPost httpPost = new HttpPost(url); | ||||
| StringEntity entityStr = new StringEntity(xmlStr); | StringEntity entityStr = new StringEntity(xmlStr); | ||||
| entityStr.setContentType("text/xml"); | entityStr.setContentType("text/xml"); | ||||
| System.out.println("entityStr--------------"+entityStr); | |||||
| //logger.info("entityStr--------------"+entityStr); | |||||
| httpPost.setEntity(entityStr); | httpPost.setEntity(entityStr); | ||||
| CloseableHttpResponse response = httpclient.execute(httpPost); | CloseableHttpResponse response = httpclient.execute(httpPost); | ||||
| try { | try { | ||||
| HttpEntity entity = response.getEntity(); | HttpEntity entity = response.getEntity(); | ||||
| System.out.println("----------------------------------------"); | |||||
| System.out.println(response.getStatusLine()); | |||||
| //System.out.println("----------------------------------------"); | |||||
| //System.out.println(response.getStatusLine()); | |||||
| if (entity != null) { | if (entity != null) { | ||||
| System.out.println("Response content length: " + entity.getContentLength()); | |||||
| //System.out.println("Response content length: " + entity.getContentLength()); | |||||
| BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); | ||||
| StringBuilder sb = new StringBuilder(); | StringBuilder sb = new StringBuilder(); | ||||
| String line = null; | String line = null; | ||||