URLConnection을 확장(상속)한 클래스이며 HTTP 프로토콜을 위한것이라고 보시면 됩니다. 생성자는 다음과
같습니다. HttpURLConnection(URL u) -- Constructor for the HttpURLConnection
------------------ 예제1 ------------------ import
java.io.*; import java.net.*; class HttpUrlConnectionTest{ public
static void main(String[] args) throws Exception { try{ URL aURL = new
URL("http", "www.oraclejavanew.kr", 80 , "/index.html"); HttpURLConnection
connection = (HttpURLConnection)aURL.openConnection(); int responseCode =
connection.getResponseCode();
if (responseCode ==
HttpURLConnection.HTTP_OK) { System.out.println("Request Method : " +
connection.getRequestMethod()); System.out.println("Request Code : " +
connection.getResponseCode()); System.out.println("Request Message : " +
connection.getResponseMessage()); BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream())); //BufferedInputStream bi =
new BufferedInputStream(connection.getInputStream()); //int b;
//while((b = bi.read()) != -1) { //System.out.write(b); //}
String line; while ((line = in.readLine()) != null) {
//System.out.println(line); } } } catch(IOException e) {
System.out.println("IOException :"+ e); } } }
------------------- 예제2 -------------------
import java.io.*; import java.net.*; class HttpUrlConnectionTest2{
public static void main(String[] args) throws IOException { try{ URL
url = new URL("http", "www.neonet.co.kr", 80,
"/rebank/common/login_after_new.neo?/rebank/index.neo"); HttpURLConnection
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true); //connection.setRequestMethod("POST");
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("id=" + URLEncoder.encode("mylife68", "UTF-8"));
out.println("&passwd=" + URLEncoder.encode("tatata", "UTF-8"));
System.out.println(connection.getResponseCode());
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream())); Writer file = new
FileWriter("test.html"); String line; while ((line = in.readLine()) !=
null) { System.out.println(line); file.write(line);
} in.close(); out.close(); } catch(IOException e) {
System.out.println("IOException :"+ e); } } }
|
|
댓글 없음:
댓글 쓰기