java로 web페이지에 login을 해서 session을 유지하면서
data를 form방식으로 입력하는 예제입니다.
(login페이지는 ajax/json리턴 방식이고, data add 페이지는 일반적인 form방식을 가정)
login세션 유지는 cookie에서 JESSIONID를 읽어서 다시 세팅해야 합니다.
data는 file에서 하나씩 읽어서 web상으로 입력하는 형태입니다.
=========java source=====================
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class LoginAndInsert {
public static void main(String[] args) throws Exception{
//LOG IN
URL url = new URL("http://221.100.200.180/loginProc");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches( false );
conn.setConnectTimeout(1000);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setRequestMethod("POST");
//data setting
String postData = "user_id=admin" + "&user_pwd=" + "a123456789";
System.out.println(postData.toString());
conn.setRequestProperty("Content-Length", String.valueOf(postData.getBytes("UTF-8").length));
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.setDoOutput(true); //POST
conn.connect();
OutputStream outputStream = conn.getOutputStream();
outputStream.write(postData.getBytes("UTF-8"));
outputStream.close();
InputStream inputStream = null;
try {
inputStream = conn.getInputStream();
}catch (IOException ioe){
ioe.printStackTrace();
}
String m_cookies = "";
String line3;
BufferedReader reader = new BufferedReader(new
InputStreamReader( inputStream ));
while ((line3 = reader.readLine()) != null) {
System.out.println(line3);
}
//save Cookie
Map<String, List<String>> imap = conn.getHeaderFields( ) ;
System.out.println(imap.size());
if( imap.containsKey( "Set-Cookie" ) ) //"Set-Cookie"
{
List<String> lString = imap.get( "Set-Cookie"); //"Set-Cookie" );
for( int i = 0 ; i < lString.size() ; i++ )
m_cookies += lString.get( i ) ;
}
System.err.println("Cookie:"+ m_cookies);
//find JSESSIONID
String[] cookieTokens = m_cookies.split(";");
String JSESSIONID = cookieTokens[1].substring( cookieTokens[1].indexOf("D=")+2 );
System.err.println("JSESSIONID:"+ JSESSIONID);
reader.close();
Thread.sleep(500);
//Data process
Scanner in = new Scanner(new File("./dataFile.txt"));
int i=0;
while ( in.hasNextLine() ){
String line = in.nextLine();
//System.out.println(line);
String[] token = line.split(",");
String id = token[0];
String phone = token[1];
String name = token[2];
String pw = "tel" + phone.substring(3);
if (id.length() < 4) System.err.println(id);
if (pw.length() < 10) System.err.println(pw);
//INSERT by POST
url = new URL("http://221.148.199.79/admin/normalUserMng/registProc");
conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches( false );
conn.setConnectTimeout(1000);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setRequestMethod("POST");
conn.setRequestProperty( "Cookie", "JSESSIONID="+JSESSIONID );
conn.setInstanceFollowRedirects(false); //For cookie
//data Setting
postData = "act=R"
+ "&user_id=" + id
+ "&user_pwd=" + pw
+ "&user_name=" + name
+ "&user_phone=" + phone
+ "&is_active=1"
+ "&user_domain=ktait.com";
conn.setRequestProperty("Content-Length", String.valueOf(postData.getBytes("UTF-8").length));
System.out.println(i++ + "====== "+name + "," + id + "," + phone + "," + pw +", "+ postData);
conn.setDoOutput(true); //POST
//conn.setDoInput(true); //POST
conn.connect();
outputStream = conn.getOutputStream();
outputStream.write(postData.getBytes("UTF-8"));
outputStream.close();
String line2;
reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
while ((line2 = reader.readLine()) != null) {
System.out.print(line2);
}
reader.close();
System.out.println("");
Thread.sleep(100);
}
}
}