| | |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.http.*; |
| | | import org.apache.http.client.ClientProtocolException; |
| | | 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; |
| | |
| | | } |
| | | return result; |
| | | } |
| | | /** |
| | | * 发送HttpPost请求,参数为map |
| | | * @param url |
| | | * @param jsonDataStr |
| | | * @return |
| | | */ |
| | | public static R sendPost(String url, String jsonDataStr,Map<String,String> headers) { |
| | | R r=new R(); |
| | | // JsonObject formparams = new JsonObject(); |
| | | // for (Map.Entry<String, String> entry : map.entrySet()) { |
| | | // formparams.add(entry.getKey(), entry.getValue(); |
| | | // } |
| | | //json 格式 |
| | | //UrlEncodedFormEntity entity = new UrlEncodedFormEntity(jsonDataStr, Consts.UTF_8); |
| | | // System.out.println(jsonObject.toString()); |
| | | StringEntity entity = new StringEntity(jsonDataStr, Consts.UTF_8); |
| | | HttpPost httppost = new HttpPost(url); |
| | | /* |
| | | * 添加请求头信息 |
| | | */ |
| | | if(headers!=null&&headers.size()>0) { |
| | | for (Map.Entry<String, String> entry : headers.entrySet()) { |
| | | httppost.addHeader(entry.getKey(), entry.getValue()); |
| | | } |
| | | } |
| | | httppost.setEntity(entity); |
| | | CloseableHttpResponse response = null; |
| | | try { |
| | | response = httpclient.execute(httppost); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | HttpEntity entity1 = response.getEntity(); |
| | | String result = null; |
| | | try { |
| | | result = EntityUtils.toString(entity1, "UTF-8"); |
| | | // 使用Apache提供的工具类进行转换成字符串 |
| | | if(StringUtils.isNotBlank(result)){ |
| | | r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class); |
| | | } |
| | | } catch (ParseException | IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return r; |
| | | } |
| | | /** |
| | | * 发送HttpPost请求,参数为map |
| | | * @param url |
| | | * @param map |
| | | * @return |
| | | */ |
| | | public static R sendPost(String url, Map<String,String> map,Map<String,String> headers) { |
| | | R r=new R(); |
| | | // JsonObject formparams = new JsonObject(); |
| | | // for (Map.Entry<String, String> entry : map.entrySet()) { |
| | | // formparams.add(entry.getKey(), entry.getValue(); |
| | | // } |
| | | //json 格式 |
| | | // UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); |
| | | |
| | | JsonObject jsonObject = new JsonObject(); |
| | | for(Map.Entry entry:map.entrySet()){ |
| | | // System.out.println(entry.getKey()+ "###########" + entry.getValue()); |
| | | jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString()); |
| | | } |
| | | // System.out.println(jsonObject.toString()); |
| | | StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8); |
| | | HttpPost httppost = new HttpPost(url); |
| | | /* |
| | | * 添加请求头信息 |
| | | */ |
| | | if(headers!=null&&headers.size()>0) { |
| | | for (Map.Entry<String, String> entry : headers.entrySet()) { |
| | | httppost.addHeader(entry.getKey(), entry.getValue()); |
| | | } |
| | | } |
| | | httppost.setEntity(entity); |
| | | CloseableHttpResponse response = null; |
| | | try { |
| | | response = httpclient.execute(httppost); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | HttpEntity entity1 = response.getEntity(); |
| | | String result = null; |
| | | try { |
| | | result = EntityUtils.toString(entity, "UTF-8"); |
| | | // 使用Apache提供的工具类进行转换成字符串 |
| | | if(StringUtils.isNotBlank(result)){ |
| | | r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class); |
| | | } |
| | | } catch (ParseException | IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return r; |
| | | } |
| | | /** |
| | | * 发送HttpPost请求,参数为map |
| | | * @param url |