JAVA언어 페이지 요청에서부터 쿠키셋팅 POST 보내기 천상의날개 2010. 3. 5. 17:57 페이지 요청에서부터 쿠키셋팅 POST 보내기 package test; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class URLCookiePostTest { public static void main(String []arg){ try { URL u =new URL("http://33.230.211.204:8080/myTestProject/JSP/1.jsp"); URLConnection uc= u.openConnection(); uc.setRequestProperty("contentType", "text/html; charset=EUC-KR"); uc.setDoOutput(true); //아래 주석문 쿠키내용 체크!! /*String headerName=null; String cookie=null; for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) { System.out.println(headerName+" "+i); if (headerName.equals("Set-Cookie")) { cookie = uc.getHeaderField(i); System.out.println(cookie+" ㅋㅋ"); } }*/ //쿠키값 셋팅 String myCookies = "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true"; uc.setRequestProperty("Cookie", myCookies); //uc.connect(); //이건 해당 URL로 바로 서비스 요청하는것!! //POST로 보낼 파라메터갑 셋팅 StringBuffer parameter = new StringBuffer(); parameter.append("type=xml"); parameter.append("&ss="+ URLEncoder.encode("가나다", "euc-kr")); parameter.append("&domain=daou.co.kr"); //웹써버에 요청! OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream()); wr.write(parameter.toString()); wr.flush(); //파일에 다시씀 InputStream is= uc.getInputStream(); int value=0; FileOutputStream fos=new FileOutputStream("c:\\testhtml.html"); while((value=is.read())!=-1){ fos.write(value); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("프로그램완료!!"); } }