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