ÃֽŠ°Ô½Ã±Û(JAVA)
2019.01.13 / 22:19

[HttpClient]HttpClient¸¦ ÀÌ¿ëÇØ request º¸³»±â

hanulbit
Ãßõ ¼ö 256
HttpClient¸¦ ÀÌ¿ëÇØ GET request º¸³»±â


Apache HttpClient¸¦ ÀÌ¿ëÇÏ¸é °£ÆíÇÏ°Ô HTTP request¸¦ º¸³¾ ¼ö ÀÖ½À´Ï´Ù.

°£È¤ À¥ ¼­¹ö¸¦ ¸¸µé¸é¼­ ´Ù¸¥ ¼­¹ö·Î º¸ÅÍ request¸¦ º¸³» response ¹Þ¾Æ µ¥ÀÌÅ͸¦ ó¸®Çؾß

ÇÒ ¶§°¡ Àִµ¥ ÀÌ ¶§ HttpClient¸¦ ÀÌ¿ëÇÏ¸é °£´ÜÇÏ°Ô ±¸Çö °¡´ÉÇÕ´Ï´Ù.



MavanÀ» ÀÌ¿ëÇÑ dependency ¼³Á¤


<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.4</version>
</dependency>


*Maven°ú °°Àº ºôµåÅø ÀÌ¿ëÇÏÁö ¾Ê´Â´Ù¸é https://hc.apache.org/downloads.cgiÀ» ÅëÇØ Á÷Á¢ jarÆÄÀÏÀ» ´Ù¿î ÈÄ 

ºôµå Æнº¿¡ Ãß°¡ÇØÁÖ½Ã¸é µË´Ï´Ù.



Dependency ¼³Á¤À» Çϼ̴ٸé ÀÌÁ¦ ¾î¶² ¼ø¼­´ë·Î ÄÚµùÇÒÁö »ìÆ캸°Ú½À´Ï´Ù.


1. ClosableHttpClient ÀνºÅϽº¸¦ »ý¼º

2. HTTP request type¿¡ µû¸¥ HttpGet/HttpPost ÀνºÅϽº »ý¼º

3. addHeader ¸Þ¼­µå¸¦ ÀÌ¿ëÇØ Çì´õÁ¤º¸ Ãß°¡

4. request¸¦ executeÇؼ­ CloseableHttpResponse ¾ò±â

5. response Ãâ·Â

6. close ¸Þ¼­µå¸¦ ÅëÇØ ¿¬°á Á¾·á




1. ClosableHttpClient ÀνºÅϽº »ý¼º

1
2
3
4
5
6
7
8
9
10
11
public class Httpclient1 {
    
    private static final String USER_AGENT = "Mozila/5.0";
    private static final String GET_URL = "http://www.google.com";    
 
    public static void sendGet() throws ClientProtocolException, IOException {
        
        //http client »ý¼º
        CloseableHttpClient httpClient = HttpClients.createDefault();
    }
}
cs


2. HTTP request type¿¡ µû¸¥ HttpGet/HttpPost ÀνºÅϽº »ý¼º


1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Httpclient1 {
    
    private static final String USER_AGENT = "Mozila/5.0";
    private static final String GET_URL = "http://www.google.com";    
 
    public static void sendGet() throws ClientProtocolException, IOException {
        
        //http client »ý¼º
        CloseableHttpClient httpClient = HttpClients.createDefault();
 
        //get ¸Þ¼­µå¿Í URL ¼³Á¤
        HttpGet httpGet = new HttpGet(GET_URL);
    }
}
cs



3. addHeader ¸Þ¼­µå¸¦ ÀÌ¿ëÇØ Çì´õÁ¤º¸ Ãß°¡


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Httpclient1 {
    
    private static final String USER_AGENT = "Mozila/5.0";
    private static final String GET_URL = "http://www.google.com";    
 
    public static void sendGet() throws ClientProtocolException, IOException {
        
        //http client »ý¼º
        CloseableHttpClient httpClient = HttpClients.createDefault();
 
        //get ¸Þ¼­µå¿Í URL ¼³Á¤
        HttpGet httpGet = new HttpGet(GET_URL);
 
        //agent Á¤º¸ ¼³Á¤
        httpGet.addHeader("User-Agent", USER_AGENT);
    }
}
cs



¿©±â±îÁö ÇßÀ¸¸é ÀÌÁ¦´Â excute¸Þ¼­µå¸¦ ÅëÇØ request¸¦ º¸³»ÁÖ°í ¹ÞÀº ÀÀ´äÀ» ó¸®ÇÏ¸é µË´Ï´Ù.


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
32
33
34
35
36
37
38
39
40
41
public class Httpclient1 {
    
    private static final String USER_AGENT = "Mozila/5.0";
    private static final String GET_URL = "http://www.google.com";    
 
    public static void sendGet() throws ClientProtocolException, IOException {
        
        //http client »ý¼º
        CloseableHttpClient httpClient = HttpClients.createDefault();
 
        //get ¸Þ¼­µå¿Í URL ¼³Á¤
        HttpGet httpGet = new HttpGet(GET_URL);
 
        //agent Á¤º¸ ¼³Á¤
        httpGet.addHeader("User-Agent", USER_AGENT);
        
        //get ¿äû
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        
        System.out.println("::GET Response Status::");
        
        //responseÀÇ status ÄÚµå Ãâ·Â
        System.out.println(httpResponse.getStatusLine().getStatusCode());
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));
 
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        
        reader.close();
 
        //Print result
        System.out.println(response.toString());
        httpClient.close();
    }
}
cs



½ÇÇà°á°ú




*json µ¥ÀÌÅ͸¦ GET request¸¦ ÅëÇØ ¹Þ¾Æ¿Í¾ß ÇÏ´Â °æ¿ì°¡ ¸¹½À´Ï´Ù.

±× ¶§¿¡´Â Header Á¤º¸ÀΠContent-type¿¡ 'application/json'À» Ãß°¡ÇÏ°í ¿äûÀ» º¸³»¸é µË´Ï´Ù.


¿¹Á¦ÄÚµå

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
32
33
34
35
36
37
38
39
40
import java.io.IOException;
 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class httpclient2 {
    
    private static final String USER_AGENT = "Mozila/5.0";
    private static final String GET_URL = "https://blockchain.info/ko"
            + "/rawblock/0000000000000bae09a7a393a8acd"
            + "ed75aa67e46cb81f7acaa5ad94f9eacd103";
    
    public static void sendGet() throws ClientProtocolException, IOException {
        
        //http client »ý¼º
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        //get ¸Þ¼­µå¿Í URL ¼³Á¤
        HttpGet httpGet = new HttpGet(GET_URL);
        
        //agent Á¤º¸ ¼³Á¤
        httpGet.addHeader("User-Agent", USER_AGENT);
        httpGet.addHeader("Content-type""application/json");
        
        //get ¿äû
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        
        System.out.println("GET Response Status");
        System.out.println(httpResponse.getStatusLine().getStatusCode());
        String json = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        
        System.out.println(json);
        
        httpClient.close();
    }
}
cs


½ÇÇà°á°ú



Âü°í : http://www.journaldev.com/7146/apache-httpclient-example-closeablehttpclient