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