2016-09-01 16:58:26 -05:00
|
|
|
package com.topjohnwu.magisk.utils;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2017-08-01 23:08:34 +08:00
|
|
|
import java.io.IOException;
|
2016-09-01 16:58:26 -05:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2017-01-11 17:37:35 +08:00
|
|
|
import java.util.List;
|
2016-09-01 16:58:26 -05:00
|
|
|
import java.util.Map;
|
|
|
|
|
2017-01-11 17:37:35 +08:00
|
|
|
public class WebService {
|
2016-09-01 16:58:26 -05:00
|
|
|
|
2017-08-01 23:08:34 +08:00
|
|
|
public static String getString(String url) {
|
|
|
|
return getString(url, null);
|
2016-09-06 16:54:08 -05:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:08:34 +08:00
|
|
|
public static String getString(String url, Map<String, String> header) {
|
2017-10-01 01:12:45 +08:00
|
|
|
HttpURLConnection conn = request(url, header);
|
|
|
|
if (conn == null) return "";
|
|
|
|
return getString(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getString(HttpURLConnection conn) {
|
2017-08-01 23:08:34 +08:00
|
|
|
try {
|
2017-10-01 01:12:45 +08:00
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
2018-05-06 02:51:23 +08:00
|
|
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
|
|
|
|
int len;
|
|
|
|
char buf[] = new char[4096];
|
|
|
|
while ((len = br.read(buf)) != -1) {
|
|
|
|
builder.append(buf, 0, len);
|
|
|
|
}
|
2017-10-01 01:12:45 +08:00
|
|
|
}
|
2017-08-01 23:08:34 +08:00
|
|
|
}
|
2017-10-01 01:12:45 +08:00
|
|
|
conn.disconnect();
|
|
|
|
return builder.toString();
|
2017-08-01 23:08:34 +08:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-10-01 01:12:45 +08:00
|
|
|
return "";
|
2017-08-01 23:08:34 +08:00
|
|
|
}
|
2016-09-01 16:58:26 -05:00
|
|
|
}
|
|
|
|
|
2017-10-01 01:12:45 +08:00
|
|
|
public static HttpURLConnection request(String address, Map<String, String> header) {
|
2016-09-01 16:58:26 -05:00
|
|
|
try {
|
2017-08-01 23:08:34 +08:00
|
|
|
URL url = new URL(address);
|
2016-09-01 16:58:26 -05:00
|
|
|
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
conn.setReadTimeout(15000);
|
|
|
|
conn.setConnectTimeout(15000);
|
|
|
|
|
2017-01-11 17:37:35 +08:00
|
|
|
if (header != null) {
|
|
|
|
for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
|
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 01:12:45 +08:00
|
|
|
conn.connect();
|
|
|
|
|
|
|
|
if (header != null) {
|
|
|
|
header.clear();
|
|
|
|
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
|
|
|
|
List<String> l = entry.getValue();
|
|
|
|
header.put(entry.getKey(), l.get(l.size() - 1));
|
2017-04-26 00:14:01 +08:00
|
|
|
}
|
2016-09-01 16:58:26 -05:00
|
|
|
}
|
2017-10-01 01:12:45 +08:00
|
|
|
|
|
|
|
return conn;
|
2016-09-01 16:58:26 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2017-10-01 01:12:45 +08:00
|
|
|
return null;
|
2016-09-01 16:58:26 -05:00
|
|
|
}
|
2017-01-11 17:37:35 +08:00
|
|
|
}
|
2016-09-01 16:58:26 -05:00
|
|
|
}
|