develog

GET, POST 전송 본문

Dev/Java

GET, POST 전송

냐옴 2013. 5. 14. 18:28

public void sendGet() throws Exception {

    System.out.println(">> sendGet()");

    

    String requestURL = "http://testdomain.com/test.jsp?enc_id=AA&enc_pw=BB";

    URL url = new URL(requestURL);

    

    URLConnection urlConn = url.openConnection();

    urlConn.setUseCaches(false);

    urlConn.setDoInput(true);

    urlConn.setDoOutput(false);

    

    BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

    String line = null;

    while ((line = br.readLine()) != null) {

        System.out.println(line);

    }

    

    br.close();

}


public void sendPost() throws Exception {

    System.out.println(">> sendPost()");

    

    String requestURL = "http://testdomain.com/test.jsp";

    URL url = new URL(requestURL);

    

    URLConnection urlConn = url.openConnection();

    urlConn.setUseCaches(false);

    urlConn.setDoInput(true);

    urlConn.setDoOutput(true);

    

    OutputStreamWriter osw = new OutputStreamWriter(urlConn.getOutputStream());

    osw.write("&enc_id=CC");

    osw.write("&enc_pw=DD");

    osw.flush();

    osw.close();

    

    BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

    String line = null;

    while ((line = br.readLine()) != null) {

        System.out.println(line);

    }

    

    br.close();

}


'Dev > Java' 카테고리의 다른 글

Collection List<Map> sort  (0) 2013.05.16
Java API for RESTful Web Services  (0) 2013.05.15
REST Tutorial  (0) 2013.05.13
자바 web to web 통신  (0) 2013.05.07
synchronized  (0) 2013.03.17
Comments