HttpURLConnection 사용례

프로그래밍/Java 2009. 1. 6. 13:29 Posted by galad
일반 자바 어플서 특정 URL로 request를 날리는 방법
###########################################################################################
package test.xml;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class XMLSend {
    private String targetUrl = null;
   
    public XMLSend(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public void send(ArrayList<String[]> dataList) throws IOException {
        URL url = new URL(this.targetUrl);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
       
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setUseCaches(false);
        uc.setRequestMethod("POST");
       
        StringBuffer sb = new StringBuffer();
       
        for(int i = 0; i < dataList.size(); i++) {
            String[] s = dataList.get(i);
           
//            uc.setRequestProperty(s[1], s[2]);
               // 0 = 찾을 노드의 xpath, 1 = SAP에 보낼 파라미터명, 2 = 찾은 데이타 // 왜 안되는가?
           
            sb.append("&").append(s[1]).append("=").append(s[2]);
            // 0 = 찾을 노드의 xpath, 1 = SAP에 보낼 파라미터명, 2 = 찾은 데이타
        }
       
//        uc.connect(); // 이 방식은 왜 안되는가??
       
       
        PrintWriter pw = new PrintWriter(uc.getOutputStream());
       
        pw.write("url="+url);
        pw.write(sb.toString());
       
        pw.flush();
        pw.close();
       
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String str = null;
        while((str = br.readLine()) != null) {
            System.out.println(str);
        }
    }
}
###########################################################################################

위에서 날린 request를 서블릿서 처리하는 예시
###########################################################################################
package test.xml;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DIServer extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String a = request.getParameter("//DATETIME[@type='T' and @qualifier='DOCUMENT']/YEAR");
           // 이건 특수문자에 띄어쓰기까지 있어서 데이터를 념겨받을 수 없다
//        String a = request.getParameter("sap_element_01");
//        String a = request.getParameter("sap_element_02");
//        String a = request.getParameter("sap_element_03");
       
//        System.out.println(a);
       
        PrintWriter w = response.getWriter();
        w.println(request.getParameter("sap_element_01") + " " + request.getParameter("sap_element_02") + " " + request.getParameter("sap_element_03"));
        w.flush();
        w.close();
       
//        System.out.println(a);
    }
}
###########################################################################################

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

[HttpURLConnection] HttpURLConnection 에서 GET / POST 방식 사용상의 주의  (0) 2009.02.19
CLASSPATH  (0) 2009.01.06
[펌] HttpURLConnection 을 이용하세요  (0) 2009.01.02
04 UDP  (0) 2007.11.27
03 졸려....  (0) 2007.11.27