Skip to content

Commit 17789c2

Browse files
committed
Merge pull request #120 from longbai/useragent
update jdk useragent
2 parents f495e82 + cb77d54 commit 17789c2

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

src/main/java/com/qiniu/api/config/Config.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class Config {
88
public static final String CHARSET = "utf-8";
99
/**本地检测不通过、程序抛异常,设置 CallRet 的 statusCode 为此错误码*/
1010
public static final int ERROR_CODE = 0;
11-
12-
public static String USER_AGENT="qiniu java-sdk v6.0.0";
13-
11+
12+
public static final String VERSION = "6.0.5";
13+
1414
/**
1515
* You can get your accesskey from <a href="https://dev.qiniutek.com"
1616
* target="blank"> https://dev.qiniutek.com </a>
@@ -26,14 +26,14 @@ public class Config {
2626
public static String RS_HOST = "http://rs.qbox.me";
2727

2828
public static String UP_HOST = "http://up.qiniu.com";
29-
29+
3030
public static String RSF_HOST = "http://rsf.qbox.me";
31-
31+
3232
/**
3333
* HTTP连接超时的时间毫秒(ms)
3434
* Determines the timeout in milliseconds until a connection is established.
3535
* A timeout value of zero is interpreted as an infinite timeout.
36-
*
36+
*
3737
* Please note this parameter can only be applied to connections that
3838
* are bound to a particular local address.
3939
*/

src/main/java/com/qiniu/api/net/Client.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,45 @@
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
*/
2424
public 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);
@@ -58,20 +71,19 @@ public CallRet call(String url) {
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

@@ -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 {
@@ -104,7 +116,7 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
104116
}
105117

106118
/**
107-
*
119+
*
108120
* @param url
109121
* the request url
110122
* @param contentType
@@ -126,17 +138,16 @@ 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);
@@ -148,7 +159,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
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

src/main/java/com/qiniu/api/resumableio/Util.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
import com.qiniu.api.config.Config;
1111
import com.qiniu.api.net.CallRet;
12+
import com.qiniu.api.net.Client;
1213

1314
public class Util {
1415
public static HttpPost buildUpPost(String url, String token) {
15-
HttpPost post = new HttpPost(url);
16-
post.setHeader("User-Agent", Config.USER_AGENT);
16+
HttpPost post = Client.newPost(url);
1717
post.setHeader("Authorization", "UpToken " + token);
1818
return post;
1919
}
20-
20+
2121
public static CallRet handleResult(HttpResponse response) {
2222
try {
2323
StatusLine status = response.getStatusLine();
@@ -31,7 +31,7 @@ public static CallRet handleResult(HttpResponse response) {
3131
return ret;
3232
}
3333
}
34-
34+
3535
public static long crc32(byte[] data){
3636
CRC32 crc32 = new CRC32();
3737
crc32.update(data);

0 commit comments

Comments
 (0)