-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.java
More file actions
67 lines (60 loc) · 2.41 KB
/
Copy path146.java
File metadata and controls
67 lines (60 loc) · 2.41 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
package net.sinofool.alipay.demo;
import net.sinofool.alipay.AlipayHttpClient;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public final class DemoAlipayHttpClient implements AlipayHttpClient {
private String response(HttpResponse response) throws IllegalStateException, IOException {
StringBuffer buff = new StringBuffer();
InputStream content = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line = reader.readLine();
while (line != null) {
buff.append(line);
line = reader.readLine();
}
return buff.toString();
}
@Override
public String post(String host, int port, String schema, String uri, String body) {
try {
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(uri);
post.setHeader("Host", host);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity(body));
HttpResponse execute = client.execute(new HttpHost(host, port, schema), post);
return response(execute);
} catch (ClientProtocolException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public String get(String host, int port, String schema, String uri) {
try {
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(uri);
HttpResponse execute = client.execute(new HttpHost(host, port, schema), get);
return response(execute);
} catch (ClientProtocolException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}