HBlog
3/10 http/https의 response를 읽는 방법 본문
- HttpComponents를 이용하기
처음 시도했던 방법이다.
httpComponent 라이브러리를 활용하여 header값을 넣고, 응답을 받아와 읽으려했는데,
url이 https이기 때문에 응답코드만 200이 뜨고 값을 읽어올 수 없었다.
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://whatever.blah.com");
httpGet.addheader("key", value);
HttpResponse resp = client.execute(httpGet);
int statusCode = resp.getStatusLine().getStatusCode();
2.httpClient를 활용하기 - 자바11버전이상만 사용 가능
현재 자바 8버전을 이용하고 있기 때문에 실제로 적용하지는 못했다.
3. HttpsURLConnection 이용
집에서 실행해본 것.
200번 응답코드만 받아오는 게 확인.
적용해보아야 함
public static void main(String args[]) throws Exception{
URL u = new URL("https://mail.google.com/mail/");
HttpsURLConnection hc = (HttpsURLConnection)u.openConnection();
hc.setConnectTimeout(3000);
hc.setReadTimeout(5000);
System.out.println("Response Code: " + hc.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader((hc.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
}
'프로그래밍 기록 > 전반적인 것' 카테고리의 다른 글
Ajax (0) | 2022.12.17 |
---|---|
JVM의 타임존 reading (0) | 2022.12.17 |
HTML 정렬문제 (0) | 2022.12.17 |
3/31 정리본 (0) | 2022.12.17 |
3/14 API 적용 (0) | 2022.12.17 |
Comments