Skip to content

Commit 10778d4

Browse files
author
YangSen-qn
committed
api request parse host
1 parent 0cb9ec1 commit 10778d4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/java/com/qiniu/storage/Api.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import com.qiniu.http.Client;
55
import com.qiniu.http.MethodType;
66
import com.qiniu.http.RequestStreamBody;
7-
import com.qiniu.util.Auth;
8-
import com.qiniu.util.Json;
9-
import com.qiniu.util.StringMap;
10-
import com.qiniu.util.StringUtils;
7+
import com.qiniu.util.*;
118
import okhttp3.MediaType;
129
import okhttp3.MultipartBody;
1310
import okhttp3.RequestBody;
@@ -461,7 +458,16 @@ public String getHost() throws QiniuException {
461458
}
462459

463460
void setHost(String host) {
464-
this.host = host;
461+
URL tmpUrl = UrlUtils.parseHost(host);
462+
if (tmpUrl == null) {
463+
this.host = null;
464+
return;
465+
}
466+
467+
this.host = tmpUrl.getHost();
468+
if (tmpUrl.getPort() >= 0) {
469+
this.port = tmpUrl.getPort();
470+
}
465471
}
466472

467473
/**

src/main/java/com/qiniu/util/UrlUtils.java

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.qiniu.util;
22

33
import java.io.CharArrayWriter;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
46
import java.nio.charset.Charset;
57
import java.util.BitSet;
68

@@ -191,6 +193,19 @@ public static String removeHostScheme(String host) {
191193
return host;
192194
}
193195

196+
public static URL parseHost(String host) {
197+
if (StringUtils.isNullOrEmpty(host)) {
198+
return null;
199+
}
200+
201+
String tmpHost = setHostScheme(host, true);
202+
try {
203+
return new URL(tmpHost);
204+
} catch (MalformedURLException e) {
205+
return null;
206+
}
207+
}
208+
194209

195210
/**
196211
* 如果 host 包含 scheme 则优先使用 host 中包含的 scheme

0 commit comments

Comments
 (0)