Skip to content

Commit

Permalink
Merge pull request #23 from zshbleaker/master
Browse files Browse the repository at this point in the history
[PILCS-3501] Modify code due to server response
  • Loading branch information
longbai authored Apr 2, 2018
2 parents cf843f7 + 62d44c9 commit d14268f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions library/src/main/java/com/qiniu/android/dns/http/QiniuDns.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public QiniuDns(String accountId, String encryptKey, long expireTimeMs) {
public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) new URL(ENDPOINT + mAccountId
+ "/d?dn=" + (mEncryptKey == null ? domain.domain
: DES.encrypt( domain.domain + "?e=" + Long.toString((System.currentTimeMillis() + mExpireTimeMs) / 1000), mEncryptKey))
+ "&ttl=1").openConnection();
: DES.encrypt( domain.domain
+ "?e=" + Long.toString((System.currentTimeMillis()
+ mExpireTimeMs) / 1000), mEncryptKey))
+ "&ttl=1" + "&echo=1").openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(10000);
if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
Expand All @@ -48,15 +50,18 @@ public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
sb.append(line);
}
try {
JSONObject response = new JSONArray(mEncryptKey == null ? sb.toString() : DES.decrypt(sb.toString(), mEncryptKey)).optJSONObject(0);
JSONArray result = response.optJSONArray("A");
JSONArray result = mEncryptKey == null ?
new JSONObject(sb.toString()).optJSONArray("data").optJSONArray(0) :
new JSONArray(DES.decrypt(new JSONObject(sb.toString()).optString("data"),
mEncryptKey)).optJSONArray(0);
if (result.length() <= 0) {
return null;
}
int ttl = response.optInt("ttl");
Record[] records = new Record[result.length()];
for (int i = 0; i < records.length;++i) {
records[i] = new Record(result.getString(i), Record.TYPE_A, ttl, System.currentTimeMillis() / 1000);
JSONObject item = result.optJSONObject(i);
records[i] = new Record(item.optString("data"), Record.TYPE_A,
item.optInt("TTL"), System.currentTimeMillis() / 1000);
}
return records;
} catch (JSONException e) {
Expand Down

0 comments on commit d14268f

Please sign in to comment.