-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.java
More file actions
123 lines (123 loc) · 3.28 KB
/
Copy pathHttpClient.java
File metadata and controls
123 lines (123 loc) · 3.28 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
//package httpClient;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStreamWriter;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.nio.charset.StandardCharsets;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.xml.ws.Response;
//
//import org.omg.CORBA_2_3.portable.OutputStream;
//
//
//class Respense {
// final int code;
// final byte[] data;
//
// public Respense(int code, byte[] data) {
// this.code = code;
// this.data = data;
// }
//
// @Override
// public String toString(){
// StringBuilder sb = new StringBuilder(1024);
// sb.append(code).append("\n");
// String s = new String(data, StandardCharsets.UTF_8);
// if (s.length() > 1024) {
// sb.append(s.substring(0, 1024)).append("\n");
// } else {
// sb.append(s);
// }
// return sb.toString();
// }
//}
//
//
//public class HttpClient {
//
// public static void main(String[] args) {
// Response resp = get("https://www.douban.com");
// System.out.println(resp);
// Map<String,String> postMap = new HashMap<>();
// postMap.put("form_email", "test");
// postMap.put("form_password", "password");
// Response postResp = post("https://www.douban.com/accounts/login", "application/x-www-form-urlencoded",
// toFormData(postMap));
// System.out.println(postResp);
// }
//
// static Response get(String theUrl) {
// System.err.println("GET: " + theUrl);
// HttpURLConnection conn = null;
// try {
// URL url = new URL(theUrl);
// conn = (HttpURLConnection) url.openConnection();
// ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
// try (InputStream input = conn.getInputStream()) {
// byte[] buffer = new byte[1024];
// for(;;){
// int n = input.read(buffer);
// if (n == (-1)) {
// break;
// }
// responseBuffer.write(buffer, 0, n);
// }
// }
// return new Response(conn.getResponseCode(), responseBuffer);
//
// } catch (IOException e) {
// throw new RuntimeException(e);
// } finally {
// if (conn != null) {
// conn.disconnect();
// }
// }
//
// }
// static Response post(String theUrl, String contentType, String cotentData) {
// System.err.println("GET: " + theUrl);
// HttpURLConnection conn = null;
// try {
// URL url = new URL(theUrl);
// conn = (HttpURLConnection) url.openConnection();
// //
// conn.setRequestMethod("POST");
// conn.setDoInput(true);
// byte[] postData = cotentData.getBytes(StandardCharsets.UTF_8);
// conn.setRequestProperty("Content-Type", contentType);
// conn.setRequestProperty("Content-Length", String.valueOf(postData.length));
//
// try (OutputStream output = (OutputStream) conn.getOutputStream()) {
// output.write(postData);
// }
//
// ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
// try (InputStream input = conn.getInputStream()) {
// byte[] buffer = new byte[1024];
// for(;;){
// int n = input.read(buffer);
// if (n == (-1)) {
// break;
// }
// responseBuffer.write(buffer, 0, n);
// }
// }
// return new Response(conn.getResponseCode(), responseBuffer);
//
// } catch (IOException e) {
// throw new RuntimeException(e);
// } finally {
// if (conn != null) {
// conn.disconnect();
// }
// }
//
// }
//
//}