forked from androider/mahuayingshi-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTest.java
More file actions
88 lines (70 loc) · 3.43 KB
/
RequestTest.java
File metadata and controls
88 lines (70 loc) · 3.43 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
package test;
import core.AesUtil;
import core.AppInfo;
import core.HeaderInfo;
import okhttp3.*;
import okhttp3.internal.http.HttpHeaders;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* Author: liuqiang
* Date: 2018-12-21
* Time: 09:50
* Description:
*/
public class RequestTest {
public static void main(String[] args) {
httpTest();
}
public static void httpTest() {
final AppInfo appInfo = new AppInfo();
final String url = "http://api.hbzjmf.com/api/app/member/ver2/user/login/2/7?uuid=862629030227329&model=EVA-AL10Android%207.0";
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String path = request.url().uri().getPath();
String encryptToHex = AesUtil.encryptToHex(appInfo.getEncryptPackageId(), AesUtil.getKey(true));
HeaderInfo createHeaderInfo = appInfo.createHeaderInfo(path);
StringBuilder stringBuilder3 = new StringBuilder();
stringBuilder3.append(url);
stringBuilder3.append(url.contains("?") ? "&" : "?");
stringBuilder3.append("time=");
stringBuilder3.append(System.currentTimeMillis());
Request newRequest = request.newBuilder()
.url(stringBuilder3.toString())
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("accessToken", encryptToHex)
.header("X-Client-NonceStr", createHeaderInfo.getXClientNonceStr())
.header("X-Client-IP", createHeaderInfo.getXClientIP())
.header("X-Client-TimeStamp", createHeaderInfo.getXClientTimeStamp())
.header("X-Client-Version", createHeaderInfo.getXClientVersion())
.header("X-Client-Sign", createHeaderInfo.getXClientSign())
.header("X-Client-Token", "")
.header("X-Auth-Token", createHeaderInfo.getXAuthToken()).build();
return chain.proceed(newRequest);
}
})
.build();
final Request request = new Request.Builder()
.url(url)
.build();
//异步请求,虽然是异步但是这个回调在子线程中执行
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.err.println("Q_M: 请求错误" + e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String decryptHex = AesUtil.decryptHex(response.body().string(), AesUtil.getKey(false));
System.out.println("Q_M:" + decryptHex);
}
});
}
}