| | |
| | | package com.qxueyou.scc.wx.utils; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import net.sf.json.JSONString; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | |
| | | if (param != null) { |
| | | List<NameValuePair> paramList = new ArrayList<>(); |
| | | for (String key : param.keySet()) { |
| | | System.out.println(key); |
| | | System.out.println(param.get(key)); |
| | | paramList.add(new BasicNameValuePair(key, param.get(key))); |
| | | } |
| | | // 模拟表单 |
| | |
| | | return resultString; |
| | | } |
| | | |
| | | public static String doPostUrl(String url, JSONObject jsonCode) { |
| | | // 创建Httpclient对象 |
| | | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| | | CloseableHttpResponse response = null; |
| | | String resultString = ""; |
| | | try { |
| | | // 创建Http Post请求 |
| | | HttpPost httpPost = new HttpPost(url); |
| | | |
| | | String s = jsonCode.toJSONString(jsonCode); |
| | | |
| | | StringEntity entity=new StringEntity(s); |
| | | |
| | | httpPost.setEntity(entity); |
| | | // 执行http请求 |
| | | response = httpClient.execute(httpPost); |
| | | resultString = EntityUtils.toString(response.getEntity(), "utf-8"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | response.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | return resultString; |
| | | } |
| | | |
| | | public static String doPost(String url) { |
| | | return doPost(url, null); |
| | | } |