Java를 사용하여 서버 측의 특정 URL에서 HTML 컨텐츠를 얻으려면 어떻게해야합니까?
-
06-07-2019 - |
문제
Java를 사용하여 서버 측의 특정 URL에서 HTML 컨텐츠를로드 해야하는 응용 프로그램을 설계하고 있습니다. 어떻게 해결할 수 있습니까?
문안 인사,
해결책
나는 Apache Commons httpclient 라이브러리를 사용하여이를 수행했습니다. 여기를 살펴보십시오 :http://hc.apache.org/httpclient-3.x/tutorial.html
JDK HTTP 클라이언트 지원보다 기능이 풍부합니다.
다른 팁
필요한 모든 것이 URL을 읽는 경우 타사 라이브러리에 의지 할 필요가없는 경우 Java는 URL을 검색하기위한 지원을 구축했습니다.
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
PHP라면 사용할 수 있습니다 곱슬 곱슬하다, 그러나 그것은 Java이기 때문에 당신은 httpurlconnection, 방금이 질문을 알았 듯이 :
import java.io.bufferedReader; import java.io.ioexception; import java.io.inputStreamReader; import java.net.malformedurlexception; import java.net.url; import java.net.urlConnection;
public class urlconetent {public static void main (String [] args) {
URL url;
try {
// get URL content
String a="http://localhost:8080//TestWeb/index.jsp";
url = new URL(a);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
제휴하지 않습니다 StackOverflow