티스토리 뷰


    /**
     * HttpClient 간단 Test (+ 여러 IP 중에서 특정 IP 사용하기)
     * @author samE
     * @since  2009-03-03
     * @return responseCode
     * @throws Exception
     */
    public static int httpClientTest() throws Exception {
        
        String setIpAddr = "10.101.46.59";
        URL url = new URL("http://127.0.0.1:8080");
        
        HttpConnectionManager httpConnMgr = new SimpleHttpConnectionManager(); 
        HttpConnectionManagerParams httpConnMgrParams
                                           = new HttpConnectionManagerParams();
        
        // Connection Timeout 설정
        httpConnMgrParams.setConnectionTimeout(10000);
        
        // SO Timeout 설정
        httpConnMgrParams.setSoTimeout(120000);
        httpConnMgr.setParams(httpConnMgrParams);
        
        // HttpClient 객체 생성
        HttpClient httpClient = new HttpClient(httpConnMgr);

        
        PostMethod post = new PostMethod(url.toString());
        
        boolean bSet = false;
        Enumeration nets = NetworkInterface.getNetworkInterfaces();
                            
        System.out.println("----------- InetAddress List -----------");
        
        // Network Interface 개수만큼 반복 (물리적 랜카드가 여러개 장착되어 있을 때)
        while(nets.hasMoreElements()) {
            NetworkInterface net = (NetworkInterface)nets.nextElement();
            Enumeration inetAddrs = net.getInetAddresses();
            
            // InetAddress 개수만큼 반복 (하나의 랜카드에 여러 가상 IP가 설정되어 있을 때)
            while(inetAddrs.hasMoreElements()) {
                InetAddress inetAddr = (InetAddress)inetAddrs.nextElement();
                System.out.println(inetAddr.getHostAddress());
                
                // InetAddress의 주소가 요청된 IP주소와 같은 값을 가졌을 경우   
                if(setIpAddr.equals(inetAddr.getHostAddress())) {
                    System.out.println("----------------------------------------");
                    System.out.println("set ip address : " 
                                                      + inetAddr.getHostAddress());
                    System.out.println("----------------------------------------");
                    
                    // 해당 IP를 HttpClient 객체에 셋팅하고 반복문을 빠져 나감.
                    httpClient.getHostConfiguration().setLocalAddress(inetAddr);
                    bSet = true;
                    break;
                }
            }
            if(bSet) {
                break;
            }
        }
        
        // HttpHeader 설정
        post.setRequestHeader("Accept"        , "....");
        post.setRequestHeader("Authorization" , "....");
        post.setRequestHeader("SOAPAction"    , "\"ebXML\"");
        post.setRequestHeader("Content-Type"  , "multipart/related; ....");
        post.setRequestHeader("Connection"    , "keep-alive");
        
        // Execute
        return httpClient.executeMethod(post);
    }


'프로그래밍' 카테고리의 다른 글

[보안] RSA PKCS의 관련 자료  (0) 2009.03.09
[Java] JVM Memory 관련 내용 정리  (2) 2009.03.02
HTTP Response Code  (0) 2008.11.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함