fwq
2023-12-11 43d11becdfc0d7af00b10e91b962cd0c4738bbf1
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.io.BufferedReader;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.net.HttpURLConnection;
9 import java.net.URL;
10 import java.net.URLEncoder;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 public class RequestMethod {
16
17     /**
18      * 发送Post请求
19      * @param path 请求路径
20      * @param params 请求参数
21      * @param encoding 编码
22      * @return 服务器端内容
23      */
24     public static String sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
25         StringBuilder data = new StringBuilder();
26         if(params!=null && !params.isEmpty()){
27             for(Map.Entry<String, String> entry : params.entrySet()){
28                 data.append(entry.getKey()).append("=");
29                 data.append(URLEncoder.encode(entry.getValue(), encoding));
30                 data.append("&");
31             }
32             data.deleteCharAt(data.length() - 1);
33         }
34         byte[] entity = data.toString().getBytes();//生成实体数据
35         HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
36         conn.setConnectTimeout(5000);
37         conn.setRequestMethod("POST");
38         conn.setDoOutput(true);//允许对外输出数据
39         conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
40         conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
41         OutputStream outStream = conn.getOutputStream();
42         outStream.write(entity);
43         if(conn.getResponseCode() == 200){
44             return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
45         }
46         return null;
47     }
48     
49     /**
50      * 发送Post请求
51      * @param path 请求路径
52      * @param content 发送内容
53      * @param encoding 编码
54      * @return 服务器端内容
55      */
56     public static String sendPOSTRequest(String path, String content, String encoding){
57         //byte[] entity = content.toString().getBytes();//生成实体数据
58         try{
59             HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
60             conn.setConnectTimeout(5000);
61             conn.setRequestMethod("POST");
62             conn.setDoOutput(true);//允许对外输出数据
63             conn.setDoInput(true);
64             conn.setUseCaches(false);
81d503 65             conn.setRequestProperty("Charset", encoding);
5c5945 66             conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
E 67             //conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
68             OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
69             outStream.write(content);
70             outStream.flush();
71             if(conn.getResponseCode() == 200){
72                 return StreamUtils.InputStreamTOString(conn.getInputStream());
73             }
74         }catch(Exception e){
75             e.printStackTrace();
76         }
77         return null;
78     }
81d503 79
C 80     /**
81      * 发送Post请求
82      * @param path 请求路径
83      * @param content 发送内容
84      * @param header 请求头参数设置
85      * @param contentType 请求类型,默认application/x-www-form-urlencoded
86      * @param encoding 编码,默认UTF-8
87      * @return 服务器端内容
88      */
89     public static String sendPOSTRequest(String path, String content,Map<String,String> header,String contentType, String encoding){
90         //byte[] entity = content.toString().getBytes();//生成实体数据
91         try{
92             if(StringUtils.isNull(contentType)){
93                 contentType = "application/x-www-form-urlencoded";
94             }
95             if(StringUtils.isNull(encoding)){
96                 encoding = "UTF-8";
97             }
98
99             HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
100             conn.setConnectTimeout(5000);
101             conn.setRequestMethod("POST");
102             conn.setDoOutput(true);//允许对外输出数据
103             conn.setDoInput(true);
104             conn.setUseCaches(false);
105             conn.setRequestProperty("Charset", encoding);
106             conn.setRequestProperty("Content-Type",contentType);
107             if(header != null){
108                 for(String key:header.keySet()){//keySet获取map集合key的集合  然后在遍历key即可
109                     conn.setRequestProperty(key,header.get(key));
110                 }
111             }
112             //conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
113             OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
114             outStream.write(content);
115             outStream.flush();
116             if(conn.getResponseCode() == 200){
117                 return StreamUtils.InputStreamTOString(conn.getInputStream());
118             }
119         }catch(Exception e){
120             e.printStackTrace();
121         }
122         return null;
123     }
5c5945 124     
E 125     /**
126      * 发送Post请求
127      * @param path 请求路径
128      * @param entity 发送内容
129      * @param encoding 编码
130      * @return 服务器端内容
131      */
132     public static String sendPOSTRequest(String path, byte[] entity, String encoding) throws Exception{
133         System.out.println("entity:" + entity.length);
134         HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
135         conn.setConnectTimeout(5000);
136         conn.setRequestMethod("POST");
137         conn.setDoOutput(true);//允许对外输出数据
138         conn.setRequestProperty("Content-Type", "application/octet-stream");
139         conn.setRequestProperty("Accept", "application/octet-stream");
140         conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
141         OutputStream outStream = conn.getOutputStream();
142         outStream.write(entity);
143         if(conn.getResponseCode() == 200){
144             return StreamUtils.InputStreamTOString(conn.getInputStream());
145         }
146         return null;
147     }
148     
149     /**
150      * 发送GET请求
151      * @param path 请求路径
152      * @param params 请求参数
153      * @return 服务器端内容
154      */
155     public static String sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
156         // http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
157         StringBuilder url = new StringBuilder(path);
158         if(params!=null){
159             url.append("?");
160                 for(Map.Entry<String, String> entry : params.entrySet()){
161                     url.append(entry.getKey()).append("=");
162                     url.append(URLEncoder.encode(entry.getValue(), ecoding));
163                     url.append("&");
164                 }
165             url.deleteCharAt(url.length() - 1);
166         }
167         HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
168         conn.setConnectTimeout(5000);
169         conn.setRequestMethod("GET");
170         if(conn.getResponseCode() == 200){
171             return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
172         }
173         return null;
174     }
175     
176     
177     
178     public static String msg(InputStream in) throws Exception{
179         BufferedReader br = new BufferedReader(new InputStreamReader(in));
180         String line;
181         List<String> lines  = new ArrayList<String>();
182         while((line = br.readLine())!=null){
183             lines.add(line);
184         }
185         if(lines.size()>0){
186             String f = lines.get(0);
187             if(f.indexOf("200")!=-1){
188                 String e = lines.get(lines.size()-1);
189                 return e;
190             }else{
191                 return null;
192             }
193         }else{
194             return null;
195         }
196     }
197 }