1919
2020/**
2121 * 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}
2323 */
2424public class Client {
25-
26- private final static String HEADER_AGENT ="User-Agent" ;
25+
26+ private final static String HEADER_AGENT ="User-Agent" ;
2727
2828 /**
29- *
29+ *
3030 * @param post
3131 * @throws AuthException
3232 */
3333 public void setAuth (HttpPost post ) throws AuthException {
3434
3535 }
3636
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+
3751 /**
3852 * Sends a http post request to the specified url.
39- *
53+ *
4054 * @param url
4155 * the request url
4256 * @return A general response
4357 */
4458 public CallRet call (String url ) {
4559 HttpClient client = Http .getClient ();
46- HttpPost postMethod = new HttpPost (url );
47- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
60+ HttpPost postMethod = newPost (url );
4861 try {
4962 setAuth (postMethod );
5063 HttpResponse response = client .execute (postMethod );
5164 return handleResult (response );
5265 } catch (Exception e ) {
5366 e .printStackTrace ();
54- return new CallRet (400 , e );
67+ return new CallRet (Config . ERROR_CODE , e );
5568 }
5669 }
5770
5871 /**
5972 * Sends a http post request to the specified url with a list of
6073 * <code>NameValuePair<code>.
61- *
74+ *
6275 * @param url
6376 * the request url
6477 * @param nvps
6578 * @return A general response
6679 */
6780 public CallRet call (String url , List <NameValuePair > nvps ) {
6881 HttpClient client = Http .getClient ();
69- HttpPost postMethod = new HttpPost (url );
82+ HttpPost postMethod = newPost (url );
7083 try {
7184 StringEntity entity = new UrlEncodedFormEntity (nvps , "UTF-8" );
7285 entity .setContentType ("application/x-www-form-urlencoded" );
7386 postMethod .setEntity (entity );
74- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
7587 setAuth (postMethod );
7688 HttpResponse response = client .execute (postMethod );
7789
7890 return handleResult (response );
7991 } catch (Exception e ) {
8092 e .printStackTrace ();
81- return new CallRet (400 , e );
93+ return new CallRet (Config . ERROR_CODE , e );
8294 }
8395 }
8496
@@ -90,7 +102,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
90102 */
91103 public CallRet callWithBinary (String url , AbstractHttpEntity entity ) {
92104 HttpClient client = Http .getClient ();
93- HttpPost postMethod = new HttpPost (url );
105+ HttpPost postMethod = newPost (url );
94106 postMethod .setEntity (entity );
95107
96108 try {
@@ -99,12 +111,12 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
99111 return handleResult (response );
100112 } catch (Exception e ) {
101113 e .printStackTrace ();
102- return new CallRet (400 , e );
114+ return new CallRet (Config . ERROR_CODE , e );
103115 }
104116 }
105117
106118 /**
107- *
119+ *
108120 * @param url
109121 * the request url
110122 * @param contentType
@@ -126,48 +138,47 @@ public CallRet callWithBinary(String url, String contentType, byte[] body) {
126138 }
127139
128140 /**
129- *
141+ *
130142 * @param url
131143 * @param requestEntity
132144 * @return A general response format
133145 */
134146 public CallRet callWithMultiPart (String url , MultipartEntity requestEntity ) {
135- HttpPost postMethod = new HttpPost (url );
147+ HttpPost postMethod = newPost (url );
136148 postMethod .setEntity (requestEntity );
137- postMethod .setHeader (HEADER_AGENT ,Config .USER_AGENT );
138149 HttpClient client = Http .getClient ();
139-
150+
140151 try {
141152 HttpResponse response = client .execute (postMethod );
142153 return handleResult (response );
143154 } catch (Exception e ) {
144155 e .printStackTrace ();
145- return new CallRet (400 , e );
156+ return new CallRet (Config . ERROR_CODE , e );
146157 }
147158 }
148159
149160 /**
150161 * Transforms a httpresponse to user expected format.
151- *
162+ *
152163 * @param response
153164 * http response body
154165 * @return a formated general response structure
155166 */
156167 private CallRet handleResult (HttpResponse response ) {
157168 if (response == null || response .getStatusLine () == null ) {
158- return new CallRet (400 , "No response" );
169+ return new CallRet (Config . ERROR_CODE , "No response" );
159170 }
160171
161172 String responseBody ;
162173 try {
163174 responseBody = EntityUtils .toString (response .getEntity (),"UTF-8" );
164175 } catch (Exception e ) {
165176 e .printStackTrace ();
166- return new CallRet (400 , e );
177+ return new CallRet (Config . ERROR_CODE , e );
167178 }
168179
169180 StatusLine status = response .getStatusLine ();
170- int statusCode = (status == null ) ? 400 : status .getStatusCode ();
181+ int statusCode = (status == null ) ? Config . ERROR_CODE : status .getStatusCode ();
171182 return new CallRet (statusCode , responseBody );
172183 }
173184
0 commit comments