Created
December 13, 2017 13:54
-
-
Save developer-sdk/336eddb1a9935b6184eb5822b1bda154 to your computer and use it in GitHub Desktop.
HTTP Get, Post 방식 데이터 전송 테스트, 프록시 서버 처리
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sdk.java; | |
| import java.io.BufferedReader; | |
| import java.io.DataOutputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import javax.net.ssl.HttpsURLConnection; | |
| public class HttpConnectionExample { | |
| private final String USER_AGENT = "Mozilla/5.0"; | |
| public static void main(String[] args) throws Exception { | |
| HttpConnectionExample http = new HttpConnectionExample(); | |
| System.out.println("GET으로 데이터 가져오기"); | |
| http.sendGet("https://www.naver.com"); | |
| System.out.println("POST로 데이터 가져오기"); | |
| String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; | |
| http.sendPost("https://www.google.co.kr/", urlParameters); | |
| } | |
| // HTTP GET request | |
| private void sendGet(String targetUrl) throws Exception { | |
| URL url = new URL(targetUrl); | |
| HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
| con.setRequestMethod("GET"); // optional default is GET | |
| con.setRequestProperty("User-Agent", USER_AGENT); // add request header | |
| int responseCode = con.getResponseCode(); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| String inputLine; | |
| StringBuffer response = new StringBuffer(); | |
| while ((inputLine = in.readLine()) != null) { | |
| response.append(inputLine); | |
| } | |
| in.close(); | |
| // print result | |
| System.out.println("HTTP 응답 코드 : " + responseCode); | |
| System.out.println("HTTP body : " + response.toString()); | |
| } | |
| // HTTP POST request | |
| private void sendPost(String targetUrl, String parameters) throws Exception { | |
| URL url = new URL(targetUrl); | |
| HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); | |
| con.setRequestMethod("POST"); // HTTP POST 메소드 설정 | |
| con.setRequestProperty("User-Agent", USER_AGENT); | |
| con.setDoOutput(true); // POST 파라미터 전달을 위한 설정 | |
| // Send post request | |
| DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
| wr.writeBytes(parameters); | |
| wr.flush(); | |
| wr.close(); | |
| int responseCode = con.getResponseCode(); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| String inputLine; | |
| StringBuffer response = new StringBuffer(); | |
| while ((inputLine = in.readLine()) != null) { | |
| response.append(inputLine); | |
| } | |
| in.close(); | |
| // print result | |
| System.out.println("HTTP 응답 코드 : " + responseCode); | |
| System.out.println("HTTP body : " + response.toString()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sdk.java; | |
| import java.io.BufferedReader; | |
| import java.io.DataOutputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.InetSocketAddress; | |
| import java.net.Proxy; | |
| import java.net.URL; | |
| import javax.net.ssl.HttpsURLConnection; | |
| public class HttpConnectionWithProxyExample { | |
| private final String PROXY_IP = "192.168.0.1"; | |
| private final int PROXY_PORT = 8888; | |
| private final String USER_AGENT = "Mozilla/5.0"; | |
| public static void main(String[] args) throws Exception { | |
| HttpConnectionWithProxyExample http = new HttpConnectionWithProxyExample(); | |
| System.out.println("GET으로 데이터 가져오기"); | |
| http.sendGet("http://www.naver.com"); | |
| System.out.println("POST로 데이터 가져오기"); | |
| String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; | |
| http.sendPost("https://www.google.co.kr/", urlParameters); | |
| } | |
| /** | |
| * HTTP 프록시 생성 | |
| * | |
| * @param ip | |
| * @param port | |
| * @return | |
| */ | |
| private Proxy initProxy(String ip, int port) { | |
| return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)); | |
| } | |
| // HTTP GET request | |
| private void sendGet(String targetUrl) throws Exception { | |
| Proxy proxy = initProxy(PROXY_IP, PROXY_PORT); | |
| URL url = new URL(targetUrl); | |
| HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy); | |
| con.setRequestMethod("GET"); // optional default is GET | |
| con.setRequestProperty("User-Agent", USER_AGENT); // add request header | |
| int responseCode = con.getResponseCode(); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| String inputLine; | |
| StringBuffer response = new StringBuffer(); | |
| while ((inputLine = in.readLine()) != null) { | |
| response.append(inputLine); | |
| } | |
| in.close(); | |
| // print result | |
| System.out.println("HTTP 응답 코드 : " + responseCode); | |
| System.out.println("HTTP body : " + response.toString()); | |
| } | |
| // HTTP POST request | |
| private void sendPost(String targetUrl, String parameters) throws Exception { | |
| Proxy proxy = initProxy(PROXY_IP, PROXY_PORT); | |
| URL url = new URL(targetUrl); | |
| HttpsURLConnection con = (HttpsURLConnection) url.openConnection(proxy); | |
| con.setRequestMethod("POST"); // HTTP POST 메소드 설정 | |
| con.setRequestProperty("User-Agent", USER_AGENT); | |
| con.setDoOutput(true); // POST 파라미터 전달을 위한 설정 | |
| // Send post request | |
| DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
| wr.writeBytes(parameters); | |
| wr.flush(); | |
| wr.close(); | |
| int responseCode = con.getResponseCode(); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| String inputLine; | |
| StringBuffer response = new StringBuffer(); | |
| while ((inputLine = in.readLine()) != null) { | |
| response.append(inputLine); | |
| } | |
| in.close(); | |
| // print result | |
| System.out.println("HTTP 응답 코드 : " + responseCode); | |
| System.out.println("HTTP body : " + response.toString()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment