-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.java
More file actions
138 lines (113 loc) · 4.59 KB
/
HttpClient.java
File metadata and controls
138 lines (113 loc) · 4.59 KB
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
package com.janekey.httpserver.test;
import com.janekey.httpserver.util.StringUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
/**
* User: janekey
* Date: 14-11-20
* Time: 下午6:34
*/
public class HttpClient {
private HttpURLConnection connection;
private static final String BOUNDARY = "---------------------------7db15a14291cce";
private String lineSeparator;
private static final byte[] EMPTY_BUFFER = new byte[0];
public HttpClient(String url) throws IOException {
URL u = new URL(url);
connection = (HttpURLConnection) u.openConnection();
connection.setReadTimeout(3000);
connection.setConnectTimeout(0);
// lineSeparator = java.security.AccessController.doPrivileged(
// new sun.security.action.GetPropertyAction("line.separator"));
lineSeparator = "\r\n";
}
private void initHead() {
connection.setRequestProperty("Connection", "close");
// connection.setRequestProperty("Accept", accept);
// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
// if (referer != null) connection.setRequestProperty("Referer", referer);
// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
// connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
}
private void initMultipartHead() {
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
}
private void writeData(String data) throws IOException {
OutputStream outputStream = connection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(data);
writer.close();
}
private void writeData(byte[] buffer) throws IOException {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(buffer);
outputStream.close();
}
private String readData() throws IOException {
InputStream is = connection.getInputStream();
int available = is.available();
if (available != 14) System.out.println("available:" + available);
byte[] buffer = new byte[available];
String bufferString = null;
if (is.read(buffer) != -1) {
bufferString = new String(buffer);
}
is.close();
return bufferString;
}
private byte[] readOriginalData() throws IOException {
InputStream is = connection.getInputStream();
int available = is.available();
byte[] buffer = new byte[available];
if (is.read(buffer) != -1) {
return buffer;
} else {
return EMPTY_BUFFER;
}
}
public String post(String data) throws IOException {
initHead();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (data != null) {
writeData(data);
}
return readData();
}
public String get() throws IOException {
initHead();
connection.setDoOutput(false);
connection.setRequestMethod("GET");
return readData();
}
public byte[] getOriginal() throws IOException {
initHead();
connection.setDoOutput(false);
connection.setRequestMethod("GET");
return readOriginalData();
}
public String multipart(String name, String fileName, byte[] data) throws IOException {
initMultipartHead();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
//multipart/form-data body buffer
ByteBuffer buffer = ByteBuffer.allocate(data.length + 200);
StringBuilder string = new StringBuilder();
string.append("--").append(BOUNDARY).append(lineSeparator);
string.append("Content-Disposition: form-data; name=\"").append(name).append("\"; filename=\"")
.append(fileName).append("\"").append(lineSeparator);
string.append("Content-Type: image/jpeg").append(lineSeparator).append(lineSeparator);
buffer.put(StringUtil.getUTF8Bytes(string.toString()));
buffer.put(data);
string.delete(0, string.length());
string.append(lineSeparator).append("--").append(BOUNDARY).append("--").append(lineSeparator);
buffer.put(StringUtil.getUTF8Bytes(string.toString()));
writeData(buffer.array());
return readData();
}
}