| | |
| | | |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.net.URISyntaxException; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | import org.apache.commons.httpclient.SimpleHttpConnectionManager; |
| | | import org.apache.commons.httpclient.methods.GetMethod; |
| | | import org.apache.commons.httpclient.methods.PostMethod; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.ClientProtocolException; |
| | | 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.util.EntityUtils; |
| | | |
| | | public class HttpRequestUtil { |
| | | |
| | | |
| | | /*** |
| | | * get请求(带参数) |
| | | * @param url |
| | | * @return String |
| | | */ |
| | | public static String getReq(String url, Map<String, String> params) { |
| | | String result = null; |
| | | try { |
| | | URIBuilder uriBuilder = new URIBuilder(url); |
| | | Iterator maplist = params.entrySet().iterator(); |
| | | while (maplist.hasNext()) { |
| | | Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next(); |
| | | uriBuilder.addParameter(map.getKey(), map.getValue()); |
| | | } |
| | | CloseableHttpClient client = HttpClientBuilder.create().build(); |
| | | HttpPost get = new HttpPost(uriBuilder.build()); |
| | | get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); |
| | | HttpResponse response = client.execute(get); |
| | | result = EntityUtils.toString(response.getEntity(), "UTF-8"); |
| | | |
| | | } catch (URISyntaxException e) { |
| | | e.printStackTrace(); |
| | | } catch (ClientProtocolException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | public static String postRequest(String url, Map<String, String> params) { |
| | | // 构造HttpClient的实例 |
| | | HttpClient httpClient = new HttpClient(); |