회사에서 login을 주기적으로 확인하여
HTTPClient 3.0으로 로그인을 테스트를 할 수 있다.
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"); authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max }); String response = null; authpost.releaseConnection(); if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { } |
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
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"); authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max }); String response = null; authpost.releaseConnection(); if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { } |
문제는 https 다.
난 이렇게 해결했다.
HTTPClient 3.0 버젼
HttpClient client = new HttpClient(); client.getState().setCredentials( client.getHostConfiguration().setHost("i4h080.google.com", LOGON_PORT, "https"); PostMethod authpost = new PostMethod("/login.ggl"); authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max }); String response = null; authpost.releaseConnection(); if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { }
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
|
HTTPClient 2.0 버젼
HttpClient client = new HttpClient(); client.getState().setCredentials( client.getHostConfiguration().setHost("i4h080.google.com", LOGON_PORT, "https");
PostMethod authpost = new PostMethod("/login.ggl"); authpost.setRequestBody(new NameValuePair[] { nextURL, secukey, sqer,max }); String response = null; authpost.releaseConnection(); if (statuscode == HttpStatus.SC_OK && (response.indexOf("성공") > 0)) { }
HTTPClient 2.0으로도 로그인을 테스트를 할 수 있다.
|
출처: 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);
// ...