Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Vue 알아보기
- 미국 배당주
- intelliJ plugin
- 미국주식
- Spring Batch 강의
- docker mysql
- docker
- 도커
- 티스토리 광고 수익
- spring Annotation
- AES256
- python
- spring boot 시작
- 애드센스 수익
- IntelliJ
- Vue
- Spring Batch
- Vue 배우기
- JDK1.3
- 젠킨스
- 구글 애드센스 수익
- Python 기본편
- docker 명령어
- Spring
- MYSQL
- Vue 강의
- gradle
- apache log4j
- scrapy
- python 기초
Archives
나만의공간
HttpConnection Get / Post 사용법 본문
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());
}
}
'IT > JAVA' 카테고리의 다른 글
Project language level은 무엇인가 (0) | 2021.12.14 |
---|---|
Java O/X 매퍼(Mapper)를 사용한 XML 마샬링(Marshalling) 방법 (0) | 2017.08.31 |
전화번호/핸드폰 자릿수 체크해서 하이픈 넣는법 (0) | 2017.07.28 |
JAVA enum 사용법 (0) | 2017.03.16 |
DTO/Domain 속성을 Json변환시 JsonProperty를 이용하여 불필요한 도메인 제외 (1) | 2017.03.02 |
Comments