개발/Web
http session for httpclient
lipton
2017. 11. 22. 15:46
회사에서 login을 주기적으로 확인하여
HTTPClient 3.0으로 로그인을 테스트를 할 수 있다.
public class Login {
private static Log log = LogFactory.getLog(Login.class);
private static String LOGON_SITE = "i4h082.google.com";
private static int LOGON_PORT = 80;
static HttpClient client = new HttpClient();
public static void loginID() {
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); }
} |
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
public class Login {
private static Log log = LogFactory.getLog(Login.class);
private static String LOGON_SITE = "i4h082.google.com";
private static int LOGON_PORT = 80;
static HttpClient client = new HttpClient();
public static void loginID() {
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); }
} |
문제는 https 다.
난 이렇게 해결했다.
HTTPClient 3.0 버젼
HttpClient client = new HttpClient();
client.getState().setCredentials( new AuthScope("ids.google.com", 443 ), new UsernamePasswordCredentials("", "" ) );
client.getHostConfiguration().setHost("i4h080.google.com", LOGON_PORT, "https"); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); }
}
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
public class Login {
private static Log log = LogFactory.getLog(Login.class);
private static String LOGON_SITE = "i4h082.google.com";
private static int LOGON_PORT = 80;
static HttpClient client = new HttpClient();
public static void loginID() {
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); } |
|
HTTPClient 2.0 버젼
HttpClient client = new HttpClient();
client.getState().setCredentials( "realm", "ids.google.com", new UsernamePasswordCredentials("", "" ) );
client.getHostConfiguration().setHost("i4h080.google.com", LOGON_PORT, "https");
PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); }
}
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
public class Login {
private static Log log = LogFactory.getLog(Login.class);
private static String LOGON_SITE = "i4h082.google.com";
private static int LOGON_PORT = 80;
static HttpClient client = new HttpClient();
public static void loginID() {
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); PostMethod authpost = new PostMethod("/login.ggl"); NameValuePair nextURL = new NameValuePair("nxtURL","http://www.google.com/login.ggl"); NameValuePair secukey = new NameValuePair("secukey", ""); NameValuePair turtle = new NameValuePair("sqer", "knight76"); NameValuePair earthworm = new NameValuePair("max", "xa");
authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max });
String response = null; try { client.executeMethod(authpost); response = authpost.getResponseBodyAsString(); } catch (IOException ioe) { log.error(ioe, ioe); } finally { authpost.releaseConnection(); }
authpost.releaseConnection(); int statuscode = authpost.getStatusCode();
if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { System.out.println("login is successed"); } else { System.out.println("login is failed"); } | |
출처: http://knight76.tistory.com/298 [김용환 블로그(2004-2017)]
https://stackoverflow.com/questions/6272575/how-to-handle-the-session-in-httpclient-4-1
I am using the HttpClient 4.1.1 to test my server's REST API.
I can manage to login seem to work fine but when I try to do anything else I am failing.
Most likely I have a problem setting the cookie in the next request.
Here is my code currently:
HttpGet httpGet = new HttpGet(<my server login URL>);
httpResponse = httpClient.execute(httpGet)
sessionID = httpResponse.getFirstHeader("Set-Cookie").getValue();
httpGet.addHeader("Cookie", sessionID);
httpClient.execute(httpGet);
Is there a better way to manage the session/cookies setting in the HttpClient package?
The correct way is to prepare a CookieStore
which you need to set in the HttpContext
which you in turn pass on every HttpClient#execute()
call.
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...