CORE
HOME > JAVA > J2SE > CORE
2019.01.13 / 22:25

HttpClient 4.3.2 °£Æí »ç¿ë

hanulbit
Ãßõ ¼ö 241
ÃÖ±Ù HttpClient·Î °Ë»öÇÏ¸é °ÅÀÇ Apache Commons ÇÁ·ÎÁ§Æ®·Î¼­ÀÇ httpClient ¸¸ »ç¿ëÇÏ°í À־
Apache Commons ¿¡¼­ ºÐ¸® µÇ¿© µ¶¸³ ÇÁ·ÎÁ§Æ® HttpClient 4.3.2 ¹öÀüÀ¸·Î »ç¿ëÇÑ ³»¿ë ¿Ã·Á º¾´Ï´Ù.

1. Client »ý¼º
    CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpClient ¸¦ »ç¿ëÇÏ¿© ¿©·¯¹ø È£Ãâ °¡´É.
    ·Î±×ÀÎÈÄ ¼¼¼Ç °øÀ¯ °¡´É.
2. Client Á¾·á
    client.close();
3. ·Î±×ÀÎ
    HttpPost post = new HttpPost("[·Î±×ÀÎÇÒ submit url ÁÖ¼Ò]");
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    paramList.add(new BasicNameValuePair("ID", id));
    paramList.add(new BasicNameValuePair("PASSWORD", pw));
    paramList.add(new BasicNameValuePair("OTHER", "±âŸ"));
    post.setEntity(new UrlEncodedFormEntity(postParams));
    try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String line = "";
                while((line = rd.readLine()) != null) {
                    result.append(line);
                }
            }
            finally {
                response.close();
            }
     }catch(Exception e){
        e.printStackTrace();
     }
4. ·Î±×ÀÎ ÈÄ µ¥ÀÌÅÍ ¿äû
        String httpurl = "µ¥ÀÌÅÍ ¿äûÇÒ url";
        OutputStream ostream;
        HttpGet get = new HttpGet(httpurl);
        CloseableHttpResponse response;
        try {
            response = client.execute(get);
            InputStream inputStream = (InputStream)response.getEntity().getContent();
           
            /**
             * InputStreamÀ» ¹Ù·Î POI·Î Àо ó¸®
             * (µ¥ÀÌÅÍ°¡ Excel ÆÄÀÏ ÀÏ ¶§)
             */
            //Workbook wb = WorkbookFactory.create(inputStream);
            //Sheet sheet = wb.getSheetAt(0);
          

            /**
             * InputStreamÀ» ÆÄÀÏ·Î »ý¼ºÇÏ¿© ó¸®
             */
            File file = new File("D:/tmp" + dealId + ".xlsx");
            ostream = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            while(true) {
                final int len = inputStream.read(buffer);
                if(len <= 0) {
                    break;
                }
                ostream.write(buffer, 0, len);
            }
            ostream.close();
            response.close();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(InvalidFormatException e) {
            e.printStackTrace();
        }
        finally {}