-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHcurl.java
152 lines (121 loc) · 5.39 KB
/
Hcurl.java
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
import org.apache.http.client.ClientProtocolException;
// import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
// import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
public class Hcurl
{
public static void main(String args[]) throws IOException
{
Hcurl hcurl = new Hcurl();
hcurl.doGet();
// hcurl.doPost();
// hcurl.doPostBody();
}
private void doGet() throws IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
// String url = "http://m.cctvmall.com/games/cod/buymessage?skucode=8019870001";
String url = "http://api.jisuapi.com/express/query?appkey=f14073139cdf7c5e&type=auto&number=610416881782";
try {
HttpGet request = new HttpGet(url);
request.setHeader("", "");
CloseableHttpResponse response = httpclient.execute(request);
// System.out.println(response.getStatusLine());
// System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
// System.out.println(result);
// System.out.println(entity.getContent());
// System.out.println(EntityUtils.toString(entity));
//没有对象直接解析JSON对象
JSONObject jobj = JSON.parseObject(result);
System.out.println(jobj.getString("status"));
System.out.println(jobj.getString("msg"));
String results = jobj.getString("result");
// System.out.println(results);
JSONObject jobj1 = JSON.parseObject(results);
System.out.println(jobj1.getString("number"));
System.out.println(jobj1.getString("type"));
System.out.println(jobj1.getString("deliverystatus"));
System.out.println(jobj1.getString("issign"));
String lists = jobj1.getString("list");
// System.out.println(jobj1.getString("list"));
//没有对象直接解析JSON数组
JSONArray jarr = JSON.parseArray(lists);
for(int i=0,len=jarr.size();i<len;i++){
JSONObject temp= jarr.getJSONObject(i);
System.out.printf("name:%s, %s\n",temp.getString("time"), temp.getString("status"));
}
} catch(Exception e) {
System.out.println("error: " + e.getMessage());
} finally {
httpclient.close();
}
}
private void doPost() throws IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
String url = "http://local.mvc.com/";
try {
HttpPost request = new HttpPost(url);
// request.setHeader("", "");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "chen jian ping"));
request.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
CloseableHttpResponse response = httpclient.execute(request);
// System.out.println(response.getStatusLine());
// System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
System.out.println(result);
// System.out.println(entity.getContent());
// System.out.println(EntityUtils.toString(entity));
} catch(Exception e) {
System.out.println("error: " + e.getMessage());
} finally {
httpclient.close();
}
}
private void doPostBody() throws IOException
{
String uri = "http://local.mvc.com/";
List<Object> list = new ArrayList<>();
list.add("chen");
JSONObject jsonobject = new JSONObject();
jsonobject.put("username", "Li Ming");
jsonobject.put("age", 23);
jsonobject.put("list", list);
// System.out.println(jsonobject.toString());
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost request = new HttpPost(uri);
request.addHeader("Content-Type", "application/json");
request.setEntity(new StringEntity(jsonobject.toString()));
CloseableHttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
System.out.println(result);
} catch(Exception e){
} finally {
httpclient.close();
}
// RequestEntity entity = new StringRequestEntity(data.toString(), "application/json", null);
// request.setRequestEntity(entity);
}
}