| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package com.template.common.utils;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * @Author: binguo
- * @Date: 2023/8/21 星期一 14:58
- * @Description: com.repair.common.utils
- * @Version: 1.0
- */
- public class HttpUtils {
- /**
- * 发送http post请求
- *
- * @param
- *
- * @param
- *
- */
- public static String post(String url, Map<String, String> params) throws IOException {
- CloseableHttpClient httpClient = null;
- HttpPost httpPost = null;
- String re = "";
- try {
- httpClient = HttpClients.createDefault();
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(20000).setConnectTimeout(20000).build();
- httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- List<NameValuePair> ps = new ArrayList<NameValuePair>();
- for (String pKey : params.keySet()) {
- ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
- }
- httpPost.setEntity(new UrlEncodedFormEntity(ps));
- CloseableHttpResponse response = httpClient.execute(httpPost);
- HttpEntity httpEntity = response.getEntity();
- re = EntityUtils.toString(httpEntity, "utf-8");
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (httpPost != null) {
- httpPost.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return re;
- }
- /**
- * 发送post请求Https,参数是字符串
- *
- * @param
- * @return
- */
- public static String post(String url, String body) throws Exception {
- String str = "";
- CloseableHttpClient httpClient = null;
- HttpPost httpPost = null;
- try {
- httpClient = HttpClients.createDefault();
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(20000).setConnectTimeout(20000).build();
- httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- httpPost.setEntity(new StringEntity(body, "utf-8"));
- httpPost.setHeader("Content-Type", "application/json");
- httpPost.setHeader("Accept", "application/json");
- CloseableHttpResponse response = httpClient.execute(httpPost);
- HttpEntity httpEntity = response.getEntity();
- str = EntityUtils.toString(httpEntity, "utf-8");
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (httpPost != null) {
- httpPost.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return str;
- }
- /**
- * 判断User-Agent 是不是来自于手机
- * @param ua
- * @author Leemeea
- * @return
- */
- public static boolean checkAgentIsMobile(String ua) {
- String[] deviceArray = new String[] { "android", "iPhone", "ipod",
- "ipad", "blackberry", "ucweb", "windows phone" };
- if (ua == null) {
- return false;
- }
- ua = ua.toLowerCase();
- for (String string : deviceArray) {
- if (ua.indexOf(string) > 0) {
- return true;
- }
- }
- return false;
- }
- }
|