|
/* |
|
* @(#)NaverWebClient.java 1.0 2011. 7. 15 |
|
*/ |
|
package testsupport; |
|
|
|
import java.io.IOException; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
import javax.servlet.http.Cookie; |
|
|
|
import org.xml.sax.SAXException; |
|
|
|
import com.meterware.httpunit.GetMethodWebRequest; |
|
import com.meterware.httpunit.HttpUnitOptions; |
|
import com.meterware.httpunit.WebClient; |
|
import com.meterware.httpunit.WebConversation; |
|
import com.meterware.httpunit.WebForm; |
|
import com.meterware.httpunit.WebResponse; |
|
|
|
/** |
|
* @author benelog |
|
*/ |
|
public class NaverWebClient { |
|
static final String LOGIN_URL = "http://static.nid.naver.com/login.nhn"; |
|
|
|
public List<Cookie> getCookiesAfterLogin(String id, String pw) throws IOException, SAXException { |
|
WebClient client = login(id, pw); |
|
return collectCookies(client); |
|
} |
|
|
|
public String getCookieHeaderAfterLogin(String id, String pw) throws IOException, SAXException { |
|
List<Cookie> cookies = getCookiesAfterLogin(id, pw); |
|
StringBuilder cookiesHeader = new StringBuilder(); |
|
for (Cookie cookie : cookies) { |
|
cookiesHeader.append(String.format("%s=%s; ", cookie.getName(), cookie.getValue())); |
|
} |
|
|
|
return cookiesHeader.toString(); |
|
} |
|
|
|
private List<Cookie> collectCookies(WebClient webClient) { |
|
List<Cookie> cookies = new ArrayList<Cookie>(); |
|
String[] cookieNames = webClient.getCookieNames(); |
|
for (String name : cookieNames) { |
|
String value = webClient.getCookieValue(name); |
|
cookies.add(new Cookie(name, value)); |
|
} |
|
return cookies; |
|
} |
|
|
|
private WebClient login(String id, String pw) throws IOException, SAXException { |
|
HttpUnitOptions.setScriptingEnabled(false); |
|
WebConversation conv = new WebConversation(); |
|
GetMethodWebRequest request = new GetMethodWebRequest(LOGIN_URL); |
|
WebResponse loginFormRes = conv.getResource(request); |
|
WebForm form = loginFormRes.getFormWithName("frmNIDLogin"); |
|
form.setParameter("id", id); |
|
form.setParameter("pw", pw); |
|
WebResponse res = form.submit(); |
|
return res.getClient(); |
|
} |
|
|
|
} |