public
class
Request
extends
Activity {
@SuppressLint
(
"MissingSuperCall"
)
protected
void
onCreate(Bundle savedInstanceState) {
String urlString =
"https://note.shlu.fyi"
;
StringBuilder result =
new
StringBuilder();
HttpsURLConnection urlConnection =
null
;
try
{
URL url =
new
URL(urlString);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod(
"GET"
);
urlConnection.setConnectTimeout(
10000
);
urlConnection.setReadTimeout(
10000
);
int
responseCode = urlConnection.getResponseCode();
if
(responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = urlConnection.getInputStream();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(in));
String line;
while
((line = reader.readLine()) !=
null
) {
result.append(line);
}
reader.close();
in.close();
}
else
{
result.append(
"响应码:"
).append(responseCode);
}
}
catch
(Exception e) {
e.printStackTrace();
result.append(
"异常:"
).append(e.getMessage());
}
finally
{
if
(urlConnection !=
null
) {
urlConnection.disconnect();
}
}
System.out.println(result);
}
}