관리 메뉴

나만의공간

HttpConnection Get / Post 사용법 본문

IT/JAVA

HttpConnection Get / Post 사용법

밥알이 2017. 8. 31. 14:56

HttpConnection Get / Post 사용법

HttpConnection을 이용하여 Get방식과 Post방식으로 내용을 가져온다.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import javax.net.ssl.HttpsURLConnection;

public class HttpConn {

public static final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception{
HttpConn httpConn = new HttpConn();

System.out.println("HttpConnect Get");
httpConn.sendGet();

System.out.println("HttpConnect Post");
httpConn.sendPost();

System.out.println("HttpsConnect Post");
httpConn.sendPostHttps();



}


/**
* Get 방식
* @throws Exception
*/
private void sendGet() throws Exception {
String url = "http://bobr2.tistory.com/";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//전송방식
con.setRequestMethod("GET");
//Request Header 정의
con.setRequestProperty("User-Agent", USER_AGENT);
con.setConnectTimeout(10000); //컨텍션타임아웃 10초
con.setReadTimeout(5000); //컨텐츠조회 타임아웃 5총

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

Charset charset = Charset.forName("UTF-8");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),charset));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println("조회결과 : " + response.toString());
}

/**
* Post 방식
* Https
* @throws Exception
*/
private void sendPostHttps() throws Exception {

String url = "https://selfsolve.apple.com/wcResults.do";
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setConnectTimeout(10000); //컨텍션타임아웃 10초
con.setReadTimeout(5000); //컨텐츠조회 타임아웃 5총
// Send post request
con.setDoOutput(true); //항상 갱신된내용을 가져옴.
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

Charset charset = Charset.forName("UTF-8");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),charset));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}


/**
* Post 방식
* Https
* @throws Exception
*/
private void sendPost() throws Exception {

String url = "http://bobr2.tistory.com/";
String urlParameters = "?Param1=aaaa"
+"&Param2=bbbb";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setConnectTimeout(10000); //컨텍션타임아웃 10초
con.setReadTimeout(5000); //컨텐츠조회 타임아웃 5총
// Send post request
con.setDoOutput(true); //항상 갱신된내용을 가져옴.
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

Charset charset = Charset.forName("UTF-8");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),charset));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}






Comments