| | |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springblade.core.http.util.HttpUtil; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | public class arg { |
| | | public static String url="http://47.49.21.216:80"; |
| | | public static String test01(String path,Object obj) throws Exception{ |
| | | public static String url = "http://localhost:81"; |
| | | |
| | | public static String test01(String path, Object obj) throws Exception { |
| | | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| | | HttpPost httpPost = new HttpPost(path); |
| | | httpPost.setEntity(new StringEntity(JSON.toJSONString(obj), |
| | |
| | | String str = EntityUtils.toString(entity); |
| | | httpPost.clone(); |
| | | httpClient.close(); |
| | | System.out.println(str); |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * 发送POST请求,删除 |
| | | * |
| | | * @param url 目的地址 |
| | | * @param ids |
| | | * @return 远程响应结果 |
| | | */ |
| | | public static String sendPostRemoveByIds(String url, String ids) { |
| | | HashMap<String, String> parameters = new HashMap<>(1); |
| | | parameters.put("ids",ids); |
| | | // 返回的结果 |
| | | String result = ""; |
| | | // 读取响应输入流 |
| | | BufferedReader in = null; |
| | | PrintWriter out = null; |
| | | // 处理请求参数 |
| | | StringBuffer sb = new StringBuffer(); |
| | | // 编码之后的参数 |
| | | String params = ""; |
| | | try { |
| | | // 编码请求参数 |
| | | if (parameters.size() == 1) { |
| | | for (String name : parameters.keySet()) { |
| | | sb.append(name).append("=").append( |
| | | java.net.URLEncoder.encode(parameters.get(name), |
| | | "UTF-8")); |
| | | } |
| | | params = sb.toString(); |
| | | } else { |
| | | for (String name : parameters.keySet()) { |
| | | sb.append(name).append("=").append( |
| | | java.net.URLEncoder.encode(parameters.get(name), |
| | | "UTF-8")).append("&"); |
| | | } |
| | | String temp_params = sb.toString(); |
| | | params = temp_params.substring(0, temp_params.length() - 1); |
| | | } |
| | | // 创建URL对象 |
| | | java.net.URL connURL = new java.net.URL(url); |
| | | // 打开URL连接 |
| | | java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL |
| | | .openConnection(); |
| | | // 设置通用属性 |
| | | httpConn.setRequestProperty("Accept", "*/*"); |
| | | httpConn.setRequestProperty("Connection", "Keep-Alive"); |
| | | httpConn.setRequestProperty("User-Agent", |
| | | "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); |
| | | // 设置POST方式 |
| | | httpConn.setDoInput(true); |
| | | httpConn.setDoOutput(true); |
| | | // 获取HttpURLConnection对象对应的输出流 |
| | | out = new PrintWriter(httpConn.getOutputStream()); |
| | | // 发送请求参数 |
| | | out.write(params); |
| | | // flush输出流的缓冲 |
| | | out.flush(); |
| | | // 定义BufferedReader输入流来读取URL的响应,设置编码方式 |
| | | in = new BufferedReader(new InputStreamReader(httpConn |
| | | .getInputStream(), "UTF-8")); |
| | | String line; |
| | | // 读取返回的内容 |
| | | while ((line = in.readLine()) != null) { |
| | | result += line; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (out != null) { |
| | | out.close(); |
| | | } |
| | | if (in != null) { |
| | | in.close(); |
| | | } |
| | | } catch (IOException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |