Servlet - [Network] 인터페이스 웹서블릿 호출시 URLConnection 사용
목록 추천  
추천수: 124 
제 목 [Network] 인터페이스 웹서블릿 호출시 URLConnection 사용
작성자 박세청 작성일 2010/12/15 16:39


타시스템간 인터페이스시 웹서버의 서블릿을 호출 하여 데이터를 인터페이스 한다.

이경우 간단하게 사용 할 수 있는 방법은 URLConection을 이용하여 클라이언트를 구성하면 된다.

 

*주의사항*

 

호출시 반드시 get 방식이 아니 post 방식으로 데이터를 보내야 서버에서 getInputStream를 통해 데이틀 가져 올수 있다.  만약 get방식으로 호출한다면 getContentLength는 0이 될것이다.

클라이언트에서 getOutputStream을 하면 자동으로 post 방식으로 설정 되다는데 명확한게 좋으므로 일단은 httpConn.setDoOutput(true) 를 통해 post로 지정한다.

 

public static void main(String[] args) throws IOException{
  StringBuffer sbContent = new StringBuffer();
  
  OutputStreamWriter wr = null;  
  BufferedReader br = null;
  
  try{
   URL url = new URL("http://localhost:3001/process.do"); 
   URLConnection  httpConn = url.openConnection(); 
   httpConn.setDoOutput(true);
   
   wr = new OutputStreamWriter(httpConn.getOutputStream());
         wr.write("안녕하세요");
         wr.flush();
        
        
   br = new BufferedReader( new InputStreamReader(httpConn.getInputStream()));
   
      
   while( br.ready()){
    sbContent.append(br.readLine());
    sbContent.append("\n");
   }
   
  }catch(Exception e){
   sbContent.append("URL: "+e.getMessage());
  }finally{
   if(wr!=null) wr.close();
   if(br!=null) br.close();
  }
 }





이전글 이전 글이 없습니다.
다음글 간단한 업로드 서블릿(Upload Servlet) 예제

목록 추천