Skip to content

Commit fca48af

Browse files
committed
for test
1 parent cc9e1fd commit fca48af

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.janekey.httpserver.test;
2+
3+
import com.janekey.httpserver.util.StringUtil;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.io.OutputStreamWriter;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.nio.ByteBuffer;
12+
13+
/**
14+
* User: janekey
15+
* Date: 14-11-20
16+
* Time: 下午6:34
17+
*/
18+
public class HttpClient {
19+
private HttpURLConnection connection;
20+
private static final String BOUNDARY = "---------------------------7db15a14291cce";
21+
private String lineSeparator;
22+
private static final byte[] EMPTY_BUFFER = new byte[0];
23+
24+
public HttpClient(String url) throws IOException {
25+
URL u = new URL(url);
26+
connection = (HttpURLConnection) u.openConnection();
27+
// lineSeparator = java.security.AccessController.doPrivileged(
28+
// new sun.security.action.GetPropertyAction("line.separator"));
29+
lineSeparator = "\r\n";
30+
}
31+
32+
private void initHead() {
33+
// connection.setRequestProperty("Accept", accept);
34+
// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
35+
// if (referer != null) connection.setRequestProperty("Referer", referer);
36+
// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
37+
// connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
38+
}
39+
40+
private void initMultipartHead() {
41+
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
42+
}
43+
44+
private void writeData(String data) throws IOException {
45+
OutputStream outputStream = connection.getOutputStream();
46+
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
47+
writer.write(data);
48+
writer.close();
49+
}
50+
51+
private void writeData(byte[] buffer) throws IOException {
52+
OutputStream outputStream = connection.getOutputStream();
53+
outputStream.write(buffer);
54+
outputStream.close();
55+
}
56+
57+
private String readData() throws IOException {
58+
InputStream is = connection.getInputStream();
59+
int available = is.available();
60+
byte[] buffer = new byte[available];
61+
String bufferString = null;
62+
if (is.read(buffer) != -1) {
63+
bufferString = new String(buffer);
64+
}
65+
is.close();
66+
return bufferString;
67+
}
68+
69+
private byte[] readOriginalData() throws IOException {
70+
InputStream is = connection.getInputStream();
71+
int available = is.available();
72+
byte[] buffer = new byte[available];
73+
if (is.read(buffer) != -1) {
74+
return buffer;
75+
} else {
76+
return EMPTY_BUFFER;
77+
}
78+
}
79+
80+
public String post(String data) throws IOException {
81+
initHead();
82+
83+
connection.setDoOutput(true);
84+
connection.setRequestMethod("POST");
85+
86+
if (data != null) {
87+
writeData(data);
88+
}
89+
return readData();
90+
}
91+
92+
public String get() throws IOException {
93+
initHead();
94+
95+
connection.setDoOutput(false);
96+
connection.setRequestMethod("GET");
97+
98+
return readData();
99+
}
100+
101+
public byte[] getOriginal() throws IOException {
102+
initHead();
103+
104+
connection.setDoOutput(false);
105+
connection.setRequestMethod("GET");
106+
107+
return readOriginalData();
108+
}
109+
110+
public String multipart(String name, String fileName, byte[] data) throws IOException {
111+
initMultipartHead();
112+
113+
connection.setDoOutput(true);
114+
connection.setRequestMethod("POST");
115+
116+
//multipart/form-data body buffer
117+
ByteBuffer buffer = ByteBuffer.allocate(data.length + 200);
118+
119+
StringBuilder string = new StringBuilder();
120+
string.append("--").append(BOUNDARY).append(lineSeparator);
121+
string.append("Content-Disposition: form-data; name=\"").append(name).append("\"; filename=\"")
122+
.append(fileName).append("\"").append(lineSeparator);
123+
string.append("Content-Type: image/jpeg").append(lineSeparator).append(lineSeparator);
124+
125+
buffer.put(StringUtil.getUTF8Bytes(string.toString()));
126+
buffer.put(data);
127+
128+
string.delete(0, string.length());
129+
string.append(lineSeparator).append("--").append(BOUNDARY).append("--").append(lineSeparator);
130+
buffer.put(StringUtil.getUTF8Bytes(string.toString()));
131+
writeData(buffer.array());
132+
return readData();
133+
}
134+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.janekey.httpserver.test;
2+
3+
/**
4+
* User: janekey
5+
* Date: 14-11-20
6+
* Time: 下午6:30
7+
*/
8+
public class ProformaceTest {
9+
10+
public static void main(String[] args) {
11+
String url = "http://localhost:12345/";
12+
try {
13+
HttpClient httpClient = new HttpClient(url);
14+
String content = httpClient.get();
15+
System.out.println(content);
16+
} catch (Throwable th) {
17+
th.printStackTrace();
18+
}
19+
}
20+
21+
}

src/com/janekey/httpserver/util/StringUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.janekey.httpserver.util;
22

3+
import java.nio.charset.Charset;
4+
35
/**
46
* User: janekey
57
* Date: 14-11-19
68
* Time: 上午11:26
79
*/
810
public class StringUtil {
911

12+
/**UTF-8的Charset*/
13+
public static final Charset UTF_8 = Charset.forName("UTF-8");
14+
1015
public static boolean isNotEmpty(String str) {
1116
if (str != null && str.length() > 0) {
1217
for (int i = 0; i < str.length(); i++) {
@@ -22,4 +27,11 @@ public static boolean isEmpty(String str) {
2227
return !isNotEmpty(str);
2328
}
2429

30+
public static byte[] getUTF8Bytes(String s) {
31+
if (s != null && s.length() >= 0) {
32+
return s.getBytes(UTF_8);
33+
}
34+
return null;
35+
}
36+
2537
}

0 commit comments

Comments
 (0)