xiaoyong931011
2022-07-28 363c0f2d5ac2cec13e28bcba4f46f272dff448dc
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
package cc.mrbird.febs.pay.util;
 
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
 
public class HttpRequest {
    public static final String SEND_POST = "POST";
    public static final String SEND_GET = "GET";
    private HttpRequest() {
    }
 
    public static HttpRequest createHttpRequest() {
        return new HttpRequest();
    }
 
    public HttpResponse sendHttpGet(String urlString, Map<String, String> params) throws IOException {
 
        if (params != null) {
            StringBuffer param = new StringBuffer();
            int i = 0;
            for (String key : params.keySet()) {
                if (i == 0)
                    param.append("?");
                else
                    param.append("&");
                param.append(key).append("=").append((String) params.get(key));
                i++;
            }
            urlString = urlString + param;
        }
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
 
        HttpResponse response = new HttpResponse();
        response.setInputStream(urlConnection.getInputStream());
        response.setContentType(urlConnection.getContentType());
        response.setContentLength(urlConnection.getContentLength());
 
        response.setFileName(urlConnection.getRequestProperty("filename"));
        return response;
    }
 
    public HttpResponse sendHttpPost(String urlString, Map<String, String> params, String filePath, String contentType)
            throws IOException {
        if (params != null) {
            StringBuffer param = new StringBuffer();
            int i = 0;
            for (String key : params.keySet()) {
                if (i == 0)
                    param.append("?");
                else
                    param.append("&");
                param.append(key).append("=").append((String) params.get(key));
                i++;
            }
            urlString = urlString + param;
        }
 
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
 
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
 
        File file = new File(filePath);
 
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Charset", "UTF-8");
 
        String BOUNDARY = "----------" + System.currentTimeMillis();
        urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
 
        StringBuilder sb = new StringBuilder();
        sb.append("--");
        sb.append(BOUNDARY);
        sb.append("\r\n");
        sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n");
        sb.append("Content-Type:application/octet-stream\r\n\r\n");
 
        byte[] head = sb.toString().getBytes("utf-8");
 
        OutputStream out1 = new DataOutputStream(urlConnection.getOutputStream());
 
        out1.write(head);
 
        DataInputStream in1 = new DataInputStream(new FileInputStream(file));
        int bytes = 0;
        byte[] bufferOut = new byte[1024];
        while ((bytes = in1.read(bufferOut)) != -1) {
            out1.write(bufferOut, 0, bytes);
        }
        in1.close();
 
        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
 
        out1.write(foot);
 
        out1.flush();
        out1.close();
 
        FileInputStream in = new FileInputStream(file);
 
        OutputStream out = urlConnection.getOutputStream();
        byte[] buf = new byte[1024];
        int hasRead = 0;
        while ((hasRead = in.read(buf)) > 0) {
            out.write(buf, 0, hasRead);
        }
        in.close();
        out.flush();
        out.close();
 
        HttpResponse response = new HttpResponse();
        response.setInputStream(urlConnection.getInputStream());
 
        return response;
    }
 
    public HttpResponse sendHttpsGet(String urlString, Map<String, String> params) throws IOException {
        if (params != null) {
            StringBuffer param = new StringBuffer();
            int i = 0;
            for (String key : params.keySet()) {
                if (i == 0)
                    param.append("?");
                else
                    param.append("&");
                param.append(key).append("=").append((String) params.get(key));
                i++;
            }
            urlString = urlString + param;
        }
 
        URL url = new URL(urlString);
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
 
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
 
        HttpResponse response = new HttpResponse();
        response.setInputStream(urlConnection.getInputStream());
        response.setContentType(urlConnection.getContentType());
        response.setContentLength(urlConnection.getContentLength());
 
        return response;
    }
 
    public HttpResponse sendHttpsPost(String urlString, Map<String, String> params, String sendData)
            throws IOException {
        if (params != null) {
            StringBuffer param = new StringBuffer();
            int i = 0;
            for (String key : params.keySet()) {
                if (i == 0)
                    param.append("?");
                else
                    param.append("&");
                param.append(key).append("=").append((String) params.get(key));
                i++;
            }
            urlString = urlString + param;
        }
 
        URL url = new URL(urlString);
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
 
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
 
        if (sendData != null) {
            urlConnection.getOutputStream().write(sendData.getBytes("utf-8"));
            urlConnection.getOutputStream().flush();
            urlConnection.getOutputStream().close();
        }
 
        HttpResponse response = new HttpResponse();
        response.setInputStream(urlConnection.getInputStream());
        response.setContentType(urlConnection.getContentType());
        response.setContentLength(urlConnection.getContentLength());
 
        return response;
    }
}