Tools package HttpClientUtils

package cn.gzsxt.ego.base.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
org.apache.http.impl.client.LaxRedirectStrategy Import; 
Import org.apache.http.message.BasicNameValuePair; 
Import org.apache.http.util.EntityUtils; 
/ ** 
 * - sending api get request 
 * CloseableHttpClient class, client implementation class 
 * HttpClients class, client tools for creating client object. 
 * CloseableHttpResponse interface in response to a requested object 
 * UriBuilder categories: url class construct, to set the path variable get request 
 * HttpGet classes: transmission target get request 
 * process the EntityUtils entity class Class 
 * 
 * - send a request using the API post 
 * CloseableHttpClient class 
 * HttpClientBuilder client build an object, is used to create client object. 
 * LaxRedirectStrategy class policy request redirect POST 
 * request response object CloseableHttpResponse 
 * HttpPost post request transmission target 
 * of NameValuePairs class, for setting the parameter values 
 * UrlEncodedFormEntity: to form parameters for setting the transmission target HttpPost 
 * 
 * @author Ranger 
 * 
 * /
public class HttpClientUtils {

public static String doGet(String url,Map<String, String> params){
		
		//获取httpclient客户端
		CloseableHttpClient httpclient = HttpClients.createDefault();
		
		String resultString = "";
		
		CloseableHttpResponse response = null;
		
		try {
			URIBuilder builder = new URIBuilder(url);
			
			if(null!=params){
				for (String key:params.keySet()) {
					builder.setParameter(key, params.get(key));
				}
			}
			
			HttpGet get = new HttpGet(builder.build());
			
			
			response = httpclient.execute(get);
			
			System.out.println(response.getStatusLine());
			
			if(200==response.getStatusLine().getStatusCode()){
				HttpEntity entity = response.getEntity();
				resultString = EntityUtils.toString(entity, "utf-8");
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			if(null!=response){
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(null!=httpclient){
				try {
					httpclient.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} 
		
		Return ResultString; 
	}
	
	public static String the doGet (String URL) { 
		return the doGet (URL, null); 
	} 
	
	public static String the doPost (String URL, the Map <String, String> the params) { 
		/ ** 
		 * versions 4.0 and above in httpclient , post need to redirect the policy specifies that, if the press does not specify a default redirection policy. 
		 * 
		 * Get httpclient client 
		 * / 
		CloseableHttpClient httpclient = HttpClientBuilder.create () setRedirectStrategy (new new LaxRedirectStrategy ()) Build ();.. 
		
		String ResultString = ""; 
		
		CloseableHttpResponse Response = null; 
		
		the try { 
			
			
			HttpPost = POST new new HttpPost (URL) ; 
			
			List <of NameValuePairs> paramaters in the new new = the ArrayList <> (); 
			
			IF (! = null the params) {
				for (String Key: params.keySet ()) {  
					paramaters.add (new new BasicNameValuePair (Key, params.get (Key)));
				} 
				
				UrlEncodedFormEntity formEntity = new new UrlEncodedFormEntity (paramaters in the); 
				
				post.setEntity (formEntity); 
			} 
			
			
			/ ** 
			 * the HTTP / 1.1 403 Forbidden 
			 * reasons: 
			 * Some websites provided anti reptiles mechanism 
			 * the solution: 
			 * set request header, camouflage browser 
			 * / 
			post.addHeader ( "the User-Agent", "Mozilla / 5.0 (Windows NT 6.3; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 63.0. Safari 3239.132 / 537.36 "); 
			
			Response = httpclient.execute (POST); 
			
			System.out.println (response.getStatusLine ()); 
			
			. IF (response.getStatusLine 200 is == () getStatusCode ()) { 
				the HttpEntity Entity = Response. getEntity (); 
				ResultString = EntityUtils.toString (Entity, "UTF-. 8"); 
			} 
			
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			if(null!=response){
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(null!=httpclient){
				try {
					httpclient.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return resultString;
	}
	
	public static String doPost(String url){
		return doPost(url, null);
	}
 
}

  

Guess you like

Origin www.cnblogs.com/cqming/p/11266050.html