19
19
20
20
/**
21
21
* The class {@code Client} is a typical wrapper of RPC. Also see
22
- * {@code com.qiniu.api.auth.DigestAuthClient}
22
+ * {@code com.qiniu.api.auth.DigestAuthClient}
23
23
*/
24
24
public class Client {
25
-
26
- private final static String HEADER_AGENT ="User-Agent" ;
25
+
26
+ private final static String HEADER_AGENT ="User-Agent" ;
27
27
28
28
/**
29
- *
29
+ *
30
30
* @param post
31
31
* @throws AuthException
32
32
*/
33
33
public void setAuth (HttpPost post ) throws AuthException {
34
34
35
35
}
36
36
37
+ public static HttpPost newPost (String url ){
38
+ HttpPost postMethod = new HttpPost (url );
39
+ postMethod .setHeader (HEADER_AGENT , getUserAgent ());
40
+ System .out .println (getUserAgent ());
41
+ return postMethod ;
42
+ }
43
+
44
+ private static String getUserAgent (){
45
+ String javaVersion = "Java/" + System .getProperty ("java.version" );
46
+ String os = System .getProperty ("os.name" ) + " "
47
+ + System .getProperty ("os.arch" ) + " " + System .getProperty ("os.version" );
48
+ String sdk = "QiniuJava/" + Config .VERSION ;
49
+ return sdk + " (" + os +") " + javaVersion ;
50
+ }
51
+
37
52
/**
38
53
* Sends a http post request to the specified url.
39
- *
54
+ *
40
55
* @param url
41
56
* the request url
42
57
* @return A general response
43
58
*/
44
59
public CallRet call (String url ) {
45
60
HttpClient client = Http .getClient ();
46
- HttpPost postMethod = new HttpPost (url );
47
- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
61
+ HttpPost postMethod = newPost (url );
48
62
try {
49
63
setAuth (postMethod );
50
64
HttpResponse response = client .execute (postMethod );
@@ -58,20 +72,19 @@ public CallRet call(String url) {
58
72
/**
59
73
* Sends a http post request to the specified url with a list of
60
74
* <code>NameValuePair<code>.
61
- *
75
+ *
62
76
* @param url
63
77
* the request url
64
78
* @param nvps
65
79
* @return A general response
66
80
*/
67
81
public CallRet call (String url , List <NameValuePair > nvps ) {
68
82
HttpClient client = Http .getClient ();
69
- HttpPost postMethod = new HttpPost (url );
83
+ HttpPost postMethod = newPost (url );
70
84
try {
71
85
StringEntity entity = new UrlEncodedFormEntity (nvps , "UTF-8" );
72
86
entity .setContentType ("application/x-www-form-urlencoded" );
73
87
postMethod .setEntity (entity );
74
- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
75
88
setAuth (postMethod );
76
89
HttpResponse response = client .execute (postMethod );
77
90
@@ -90,7 +103,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
90
103
*/
91
104
public CallRet callWithBinary (String url , AbstractHttpEntity entity ) {
92
105
HttpClient client = Http .getClient ();
93
- HttpPost postMethod = new HttpPost (url );
106
+ HttpPost postMethod = newPost (url );
94
107
postMethod .setEntity (entity );
95
108
96
109
try {
@@ -104,7 +117,7 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
104
117
}
105
118
106
119
/**
107
- *
120
+ *
108
121
* @param url
109
122
* the request url
110
123
* @param contentType
@@ -126,17 +139,16 @@ public CallRet callWithBinary(String url, String contentType, byte[] body) {
126
139
}
127
140
128
141
/**
129
- *
142
+ *
130
143
* @param url
131
144
* @param requestEntity
132
145
* @return A general response format
133
146
*/
134
147
public CallRet callWithMultiPart (String url , MultipartEntity requestEntity ) {
135
- HttpPost postMethod = new HttpPost (url );
148
+ HttpPost postMethod = newPost (url );
136
149
postMethod .setEntity (requestEntity );
137
- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
138
150
HttpClient client = Http .getClient ();
139
-
151
+
140
152
try {
141
153
HttpResponse response = client .execute (postMethod );
142
154
return handleResult (response );
@@ -148,7 +160,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
148
160
149
161
/**
150
162
* Transforms a httpresponse to user expected format.
151
- *
163
+ *
152
164
* @param response
153
165
* http response body
154
166
* @return a formated general response structure
0 commit comments