1 public class HttpUtils { 2 3 final static Header HEADER_USER_AGENT = new BasicHeader( "user-agent" , "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2700.0 Safari/537.36"); 4 final static Header HEADER_CONNECTION = new BasicHeader( "connection" , "keep-alive"); 5 final static Header HEADER_CACHE_CONTROL = new BasicHeader( "cache-control" , "max-age=0"); 6 final static Header HEADER_ACCEPT = new BasicHeader( "accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 7 final static Header HEADER_ACCEPT_ENCODING = new BasicHeader( "accept-encoding" , "gzip, deflate, sdch"); 8 final static Header HEADER_ACCEPT_LANGUAGE = new BasicHeader( "accept-language" , "zh-CN,zh;q=0.8,en;q=0.6"); 9 /** 10 * 发送get请求 11 * @param url 请求的url链接 12 * @param charSet该链接字符编码 13 * */ 14 public static String requestGet(String url , String charSet , NameValuePair...nameValuePairs){ 15 CloseableHttpClient httpclient = HttpClients.createDefault(); 16 CloseableHttpResponse response = null ; 17 try { 18 HttpGet httpget = new HttpGet( url ); 19 httpget.setHeader( HEADER_USER_AGENT ) ; 20 httpget.setHeader( HEADER_CONNECTION ) ; 21 httpget.setHeader( HEADER_CACHE_CONTROL ) ; 22 httpget.setHeader( HEADER_ACCEPT ) ; 23 httpget.setHeader( HEADER_ACCEPT_ENCODING); 24 httpget.setHeader( HEADER_ACCEPT_LANGUAGE); 25 26 response = httpclient.execute( httpget ) ; 27 if(response.getStatusLine().getStatusCode() == 200){ 28 HttpEntity entity = response.getEntity() ; 29 return inputStreamToString( entity , charSet ); 30 } 31 } catch (Exception e) { 32 LogManager.err("requestGet:" + url , e) ; 33 }finally{ 34 try { 35 response.close(); 36 httpclient.close(); 37 } catch (Exception e) { 38 } 39 } 40 return "" ; 41 } 42 43 /** 44 * 发送post请求 45 * @param url 请求的url链接 46 * @param charSet该链接字符编码 47 * */ 48 public static String requestPost(String url , String charSet , NameValuePair...nameValuePairs){ 49 CloseableHttpClient httpclient = HttpClients.createDefault(); 50 CloseableHttpResponse response = null ; 51 try { 52 HttpPost httpPost = new HttpPost( url ); 53 httpPost.setHeader( HEADER_USER_AGENT ) ; 54 httpPost.setHeader( HEADER_CONNECTION ) ; 55 httpPost.setHeader( HEADER_CACHE_CONTROL ) ; 56 httpPost.setHeader( HEADER_ACCEPT ) ; 57 httpPost.setHeader( HEADER_ACCEPT_ENCODING); 58 httpPost.setHeader( HEADER_ACCEPT_LANGUAGE); 59 60 List<namevaluepair> formparams = Arrays.asList( nameValuePairs ); 61 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); 62 63 httpPost.setEntity( formEntity ) ; 64 response = httpclient.execute( httpPost ) ; 65 if(response.getStatusLine().getStatusCode() == 200){ 66 HttpEntity entity = response.getEntity() ; 67 return inputStreamToString( entity , charSet ); 68 } 69 } catch (Exception e) { 70 LogManager.err("requestPost:" + url , e) ; 71 }finally{ 72 try { 73 response.close(); 74 httpclient.close(); 75 } catch (Exception e) { 76 } 77 } 78 return "" ; 79 } 80 81 82 private static String inputStreamToString(HttpEntity entity , String charSet) 83 throws Exception{ 84 InputStream is = null ; 85 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 86 try { 87 is = entity.getContent() ; 88 byte[]bytes = new byte[52428800] ; 89 int buffer = -1; 90 91 while( (buffer = is.read(bytes)) != -1){ 92 baos.write(bytes, 0, buffer) ; 93 } 94 return new String(baos.toByteArray() , charSet ) ; 95 }finally{ 96 StreamUtils.closeInput( is ); 97 StreamUtils.closeOutput( baos ); 98 } 99 }