package com.nanometer.smartlab.util; import com.nanometer.smartlab.exception.BusinessException; import com.nanometer.smartlab.exception.ExceptionEnumCode; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import javax.swing.text.html.parser.Entity; import java.io.IOException; import java.net.URISyntaxException; import java.util.List; public class HttpUtil { public static String doGet(String url, List params){ try{ CloseableHttpClient client = HttpClients.createDefault(); URIBuilder builder = new URIBuilder(url); URIBuilder uriBuilder = builder.setParameters(params); HttpGet get = new HttpGet(uriBuilder.build()); CloseableHttpResponse res = client.execute(get); return EntityUtils.toString(res.getEntity(), "UTF-8"); } catch (IOException | URISyntaxException e) { e.printStackTrace(); throw new BusinessException(ExceptionEnumCode.SYS_ERR, "GET请求发生错误,请检查代码"); } } }