Browse Source

w4-张思渊-202401070104

main
zhangsiyuan 3 weeks ago
parent
commit
bff96af138
  1. 30
      project/src/project/utils/HttpUtils.java

30
project/src/project/utils/HttpUtils.java

@ -0,0 +1,30 @@
package project.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtils {
public static String getHtml(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP error code: " + responseCode);
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder html = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
return html.toString();
}
}
Loading…
Cancel
Save