1 changed files with 30 additions and 0 deletions
@ -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…
Reference in new issue