[java][ÇÁ·Ï½Ã] ³×Æ®¿öÅ© ÇÁ·Ï½Ã ¼³Á¤
ÀÚ¹Ù¿¡¼ ³×Æ®¿öÅ© ÇÁ·Î±×·¡¹ÖÀ» ÇÏ´Â Áß¿¡ ÇÁ·Ï½Ã¸¦ ¼³Á¤ÇÏ´Â ¹æ¹ýÀº
Proxy Ŭ·¡½º¸¦ ÀÌ¿ëÇÏ´Â °Í°ú ½Ã½ºÅÛ ÇÁ·ÎÆÛƼ¿¡ ÇÁ·Ï½Ã¸¦ ¼³Á¤ÇÏ´Â ¹æ¹ýÀÌ ÀÖ´Ù.
¾Æ·¡ÀÇ ¿¹¿¡¼´Â HttpURLConnection À» ÀÌ¿ëÇÏ´Â °æ¿ì¿¡´Â Proxy Ŭ·¡½º¸¦ ÀÌ¿ëÇÏ¸é °£´ÜÇÏ°Ô Ã³¸®ÇÒ ¼ö ÀÖ´Ù.
¸¸¾à ´Ù¸¥ ¿ÜºÎ ¶óÀ̺귯¸®¸¦ ÀÌ¿ëÇÏ¿© ó¸®ÇÏ´Â °æ¿ì ÇØ´ç ¶óÀ̺귯¸®¿¡ ÇÁ·Ï½Ã ¼³Á¤ÀÌ ¾ø´Ù¸é
½Ã½ºÅÛ ÇÁ·ÎÆÛƼ¸¦ ÀÌ¿ëÇÏ¿© ó¸®ÇÏ¸é µÈ´Ù.
ex) jsoup
package sdk.java.web;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
public class HttpRequest {
public static void main(String[] args) throws Exception {
sendGet();
}
// HTTP GET request
public static void sendGet() throws Exception {
String url = "http://www.naver.com";
// Proxy Ŭ·¡½º ÀÌ¿ë
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ÁÖ¼Ò", Æ÷Æ®));
// URL obj = new URL(url);
// HttpURLConnection con = (HttpURLConnection) obj.openConnection(proxy);
// ½Ã½ºÅÛ ÇÁ·ÎÆÛƼ ÀÌ¿ë
System.setProperty("http.proxyHost", "ÁÖ¼Ò");
System.setProperty("http.proxyPort", "Æ÷Æ®");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine).append(System.lineSeparator());
}
in.close();
// print result
System.out.println(response.toString());
}
}
Ãâó: https://118k.tistory.com/381 [°³¹ßÀÚ·Î »ì¾Æ³²±â]