@@ -22,7 +22,6 @@ import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpResponse ;
import org.apache.http.NameValuePair ;
import org.apache.http.NameValuePair ;
import org.apache.http.client.CookieStore ;
import org.apache.http.client.CookieStore ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.HttpClient ;
@@ -49,14 +48,7 @@ import org.apache.http.util.EntityUtils;
*/
*/
public class HttpUtils {
public class HttpUtils {
private static final CloseableHttpClient httpClient ;
public static final String CHARSET = " UTF-8 " ;
public static final String CHARSET = " UTF-8 " ;
// 采用静态代码块, 初始化超时时间配置, 再根据配置生成默认httpClient对象
static {
//10秒相应超时
RequestConfig config = RequestConfig . custom ( ) . setConnectTimeout ( 10000 ) . setSocketTimeout ( 10000 ) . setConnectionRequestTimeout ( 5000 ) . build ( ) ;
httpClient = HttpClientBuilder . create ( ) . setDefaultRequestConfig ( config ) . build ( ) ;
}
public static String doGet ( String url , Map < String , String > params ) {
public static String doGet ( String url , Map < String , String > params ) {
return doGet ( url , params , CHARSET ) ;
return doGet ( url , params , CHARSET ) ;
@@ -82,7 +74,10 @@ public class HttpUtils {
if ( StringUtils . isBlank ( url ) ) {
if ( StringUtils . isBlank ( url ) ) {
return null ;
return null ;
}
}
CloseableHttpResponse response = null ;
HttpGet httpGet = null ;
RequestConfig config = RequestConfig . custom ( ) . setConnectTimeout ( 10000 ) . setSocketTimeout ( 10000 ) . setConnectionRequestTimeout ( 5000 ) . build ( ) ;
CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . setDefaultRequestConfig ( config ) . build ( ) ;
try {
try {
if ( params ! = null & & ! params . isEmpty ( ) ) {
if ( params ! = null & & ! params . isEmpty ( ) ) {
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
@@ -95,8 +90,8 @@ public class HttpUtils {
// 将请求参数和url进行拼接
// 将请求参数和url进行拼接
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
}
}
HttpGet httpGet = new HttpGet ( url ) ;
httpGet = new HttpGet ( url ) ;
CloseableHttpResponse response = httpClient . execute ( httpGet ) ;
response = httpClient . execute ( httpGet ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
if ( statusCode ! = 200 ) {
if ( statusCode ! = 200 ) {
httpGet . abort ( ) ;
httpGet . abort ( ) ;
@@ -114,7 +109,20 @@ public class HttpUtils {
return result ;
return result ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . out . println ( " url错误::: " + e . toString ( ) ) ;
System . out . println ( " url错误::: " + e . toString ( ) ) ;
try {
response . close ( ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
} finally {
try {
response . close ( ) ;
httpClient . close ( ) ;
httpGet . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
return null ;
return null ;
}
}
@@ -146,6 +154,8 @@ public class HttpUtils {
}
}
HttpPost httpPost = new HttpPost ( url ) ;
HttpPost httpPost = new HttpPost ( url ) ;
CloseableHttpResponse response = null ;
CloseableHttpResponse response = null ;
RequestConfig config = RequestConfig . custom ( ) . setConnectTimeout ( 10000 ) . setSocketTimeout ( 10000 ) . setConnectionRequestTimeout ( 5000 ) . build ( ) ;
CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . setDefaultRequestConfig ( config ) . build ( ) ;
try {
try {
if ( pairs ! = null & & pairs . size ( ) > 0 ) {
if ( pairs ! = null & & pairs . size ( ) > 0 ) {
httpPost . setEntity ( new UrlEncodedFormEntity ( pairs , CHARSET ) ) ;
httpPost . setEntity ( new UrlEncodedFormEntity ( pairs , CHARSET ) ) ;
@@ -165,14 +175,19 @@ public class HttpUtils {
EntityUtils . consume ( entity ) ;
EntityUtils . consume ( entity ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
return result ;
return result ;
} catch ( Exception e ) {
} catch ( Exception e ) {
return " " ;
System . out . println ( " url错误::: " + e . toString ( ) ) ;
} finally {
e . printStackTrace ( ) ;
if ( response ! = nu ll )
} fina lly {
try {
try {
response . close ( ) ;
response . close ( ) ;
} catch ( IOException e ) { }
httpClient . close ( ) ;
httpPost . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
return null ;
}
}
/**
/**
@@ -187,7 +202,9 @@ public class HttpUtils {
if ( StringUtils . isBlank ( url ) ) {
if ( StringUtils . isBlank ( url ) ) {
return null ;
return null ;
}
}
CloseableHttpResponse response = null ;
HttpGet httpGet = null ;
CloseableHttpClient httpsClient = null ;
try {
try {
if ( params ! = null & & ! params . isEmpty ( ) ) {
if ( params ! = null & & ! params . isEmpty ( ) ) {
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
@@ -199,11 +216,11 @@ public class HttpUtils {
}
}
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
}
}
HttpGet httpGet = new HttpGet ( url ) ;
httpGet = new HttpGet ( url ) ;
// https 注意这里获取https内容, 使用了忽略证书的方式, 当然还有其他的方式来获取https内容
// https 注意这里获取https内容, 使用了忽略证书的方式, 当然还有其他的方式来获取https内容
CloseableHttpClient httpsClient = HttpUtils . createSSLClientDefault ( ) ;
httpsClient = HttpUtils . createSSLClientDefault ( ) ;
CloseableHttpResponse response = httpsClient . execute ( httpGet ) ;
response = httpsClient . execute ( httpGet ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
if ( statusCode ! = 200 ) {
if ( statusCode ! = 200 ) {
httpGet . abort ( ) ;
httpGet . abort ( ) ;
@@ -218,8 +235,17 @@ public class HttpUtils {
response . close ( ) ;
response . close ( ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
return result ;
return result ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . out . println ( " url错误::: " + e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
} finally {
try {
response . close ( ) ;
httpsClient . close ( ) ;
httpGet . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
return null ;
return null ;
}
}
@@ -287,6 +313,7 @@ public class HttpUtils {
CloseableHttpClient client = httpClientBuilder . build ( ) ;
CloseableHttpClient client = httpClientBuilder . build ( ) ;
client = ( CloseableHttpClient ) wrapClient ( client ) ;
client = ( CloseableHttpClient ) wrapClient ( client ) ;
HttpPost post = new HttpPost ( url ) ;
HttpPost post = new HttpPost ( url ) ;
CloseableHttpResponse res = null ;
try {
try {
StringEntity s = new StringEntity ( json , " utf-8 " ) ;
StringEntity s = new StringEntity ( json , " utf-8 " ) ;
if ( StringUtils . isBlank ( contentType ) ) {
if ( StringUtils . isBlank ( contentType ) ) {
@@ -294,14 +321,23 @@ public class HttpUtils {
}
}
s . setContentType ( contentType ) ;
s . setContentType ( contentType ) ;
post . setEntity ( s ) ;
post . setEntity ( s ) ;
HttpResponse res = client . execute ( post ) ;
res = client . execute ( post ) ;
HttpEntity entity = res . getEntity ( ) ;
HttpEntity entity = res . getEntity ( ) ;
String str = EntityUtils . toString ( entity , charset ) ;
String str = EntityUtils . toString ( entity , charset ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( str ) ) ) str = GBKtoUTF8 ( str ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( str ) ) ) str = GBKtoUTF8 ( str ) ;
return str ;
return str ;
} catch ( Exception e ) {
} catch ( Exception e ) {
e . printStackTrace ( ) ;
System . out . println ( " url错误::: " + e . toString ( ) ) ;
}
e . printStackTrace ( ) ;
} finally {
try {
res . close ( ) ;
client . close ( ) ;
post . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
return null ;
return null ;
}
}
@@ -358,6 +394,8 @@ public class HttpUtils {
return false ;
return false ;
}
}
HttpGet httpGet = new HttpGet ( url ) ;
HttpGet httpGet = new HttpGet ( url ) ;
RequestConfig config = RequestConfig . custom ( ) . setConnectTimeout ( 10000 ) . setSocketTimeout ( 10000 ) . setConnectionRequestTimeout ( 5000 ) . build ( ) ;
CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . setDefaultRequestConfig ( config ) . build ( ) ;
try {
try {
CloseableHttpResponse response = httpClient . execute ( httpGet ) ;
CloseableHttpResponse response = httpClient . execute ( httpGet ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
@@ -508,12 +546,13 @@ public class HttpUtils {
}
}
CloseableHttpResponse response = null ;
CloseableHttpResponse response = null ;
CloseableHttpClient httpClientc = null ;
try {
try {
if ( pairs ! = null & & pairs . size ( ) > 0 ) {
if ( pairs ! = null & & pairs . size ( ) > 0 ) {
httpPost . setEntity ( new UrlEncodedFormEntity ( pairs , CHARSET ) ) ;
httpPost . setEntity ( new UrlEncodedFormEntity ( pairs , CHARSET ) ) ;
}
}
CloseableHttpClient httpClientc = HttpUtils . createSSLClientCookie ( cookie ) ;
httpClientc = HttpUtils . createSSLClientCookie ( cookie ) ;
response = httpClientc . execute ( httpPost ) ;
response = httpClientc . execute ( httpPost ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
if ( statusCode ! = 200 ) {
if ( statusCode ! = 200 ) {
@@ -529,16 +568,28 @@ public class HttpUtils {
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
return result ;
return result ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . out . println ( " url错误::: " + e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
} finally {
} finally {
if ( response ! = null )
try {
try {
response . close ( ) ;
response . close ( ) ;
httpClientc . close ( ) ;
} catch ( IOException e ) { e . printStackTrace ( ) ; }
httpPost . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
return null ;
return null ;
}
}
/**
* 带有cookie和header 的get请求
* @param url
* @param params
* @param cookie
* @param header
* @return
*/
@SuppressWarnings ( " rawtypes " )
@SuppressWarnings ( " rawtypes " )
public static String doGet2 ( String url , Map < String , String > params , CookieStore cookie , Map < String , String > header , String charset ) {
public static String doGet2 ( String url , Map < String , String > params , CookieStore cookie , Map < String , String > header , String charset ) {
if ( charset = = null | | charset . trim ( ) . length ( ) < 1 ) charset = " UTF-8 " ;
if ( charset = = null | | charset . trim ( ) . length ( ) < 1 ) charset = " UTF-8 " ;
@@ -546,6 +597,9 @@ public class HttpUtils {
return null ;
return null ;
}
}
HttpGet httpGet = null ;
CloseableHttpClient httpsClient = null ;
CloseableHttpResponse response = null ;
try {
try {
if ( params ! = null & & ! params . isEmpty ( ) ) {
if ( params ! = null & & ! params . isEmpty ( ) ) {
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
List < NameValuePair > pairs = new ArrayList < NameValuePair > ( params . size ( ) ) ;
@@ -557,7 +611,7 @@ public class HttpUtils {
}
}
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
url + = " ? " + EntityUtils . toString ( new UrlEncodedFormEntity ( pairs , charset ) ) ;
}
}
HttpGet httpGet = new HttpGet ( url ) ;
httpGet = new HttpGet ( url ) ;
Set < Entry < String , String > > set = header . entrySet ( ) ;
Set < Entry < String , String > > set = header . entrySet ( ) ;
Iterator < Entry < String , String > > s = set . iterator ( ) ;
Iterator < Entry < String , String > > s = set . iterator ( ) ;
@@ -567,8 +621,8 @@ public class HttpUtils {
}
}
// https 注意这里获取https内容, 使用了忽略证书的方式, 当然还有其他的方式来获取https内容
// https 注意这里获取https内容, 使用了忽略证书的方式, 当然还有其他的方式来获取https内容
CloseableHttpClient httpsClient = HttpUtils . createSSLClientCookie ( cookie ) ;
httpsClient = HttpUtils . createSSLClientCookie ( cookie ) ;
CloseableHttpResponse response = httpsClient . execute ( httpGet ) ;
response = httpsClient . execute ( httpGet ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
if ( statusCode ! = 200 ) {
if ( statusCode ! = 200 ) {
httpGet . abort ( ) ;
httpGet . abort ( ) ;
@@ -583,8 +637,17 @@ public class HttpUtils {
response . close ( ) ;
response . close ( ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
if ( ! " UTF-8 " . equals ( getEncoding ( result ) ) ) result = GBKtoUTF8 ( result ) ;
return result ;
return result ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . out . println ( " url错误::: " + e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
} finally {
try {
response . close ( ) ;
httpsClient . close ( ) ;
httpGet . reset ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
return null ;
return null ;
}
}