웹 개발

[공유] HttpURLConnection 을이용해서 웹페이지 호출하기..(파라미터,값 전송)

노루아부지 2019. 7. 21. 00:44
<%!

// 파라미터 : 호출할페이지 , 파라미터명 , 파라미터값
private String call_jsp( String hostURL  , String param_name , String param_value)  
{
    String revc_data = "";
    String sending_data = "";
 
    URL url = null; 
    HttpURLConnection uc = null; 

    PrintWriter out= null; 
    BufferedReader in= null; 
    StringBuffer sb = new StringBuffer("");
  
    try{
        //페이지 연결
        url = new URL(hostURL); 
        uc = (HttpURLConnection)url.openConnection(); 
 
        uc.setDoOutput(true); //post방식:true
        uc.setDoInput(true); //데이터를 첨부하는 경우 true
        uc.setUseCaches(false); 
   
        //데이타 전송
        out = new PrintWriter(uc.getOutputStream()); 
        sending_data = param_name + "=" + param_value; //전송할 데이터(파라미터명=값)   
        out.println(sending_data);
        out.flush();
        out.close(); //비운다.

        //데이타 수신 
        in = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
   
        String output = "";
        while((output = in.readLine()) != null)
        {
            if(!output.equals(""))
            {
                output.trim();
                sb.append(output);
            }
        }
        in.close();

        revc_data = sb.toString();
        revc_data.trim();

    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(in  != null) try{in.close() ;}catch(Exception ex){}
        if(out  != null) try{out.close() ;}catch(Exception ex){}
    }

    return revc_data;
}
%>

 

출처 : https://blog.naver.com/sungs6031/40047297907

 

728x90
loading