Here's some snippet code how we implemented HttpClient using the MultiThreadedHttpConnectionManager...
import java.io.*; import java.util.*; import java.text.*; import java.security.Security; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.*; import org.apache.commons.httpclient.cookie.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.params.*; import org.apache.commons.httpclient.protocol.*; public class MyHttpclientExample{ public String protocol = "http"; public String host = "localhost"; public int port = 80; public String httpAuthRealm = null; public String httpAuthUsername = null; public String httpAuthPassword = null; public String trustStore = null; public String trustStoreType = "jks"; public String trustStorePassword = null; public String trustStoreProvider = "SUN"; public String trustStoreAlgorithm = "SunX509"; public String keyStore = null; public String keyStoreType = "jks"; public String keyStorePassword = null; public String keyStoreProvider = "SUN"; public String keyStoreAlgorithm = "SunX509"; public String path = null; public ByteArrayOutputStream responseAsStream = null; public String saveAsFilename = null; private Hashtable parameters = null; private Hashtable cookies = null; public HttpClient httpClient = null; Protocol myProtocol = null; AuthSSLProtocolSocketFactory authSSLProtocolSocketFactory = null; Cookie cookie = null; NameValuePair[] nameValuePairs = null; String s1 = null; String s2 = null; String s3 = null; int i1 = 0; int i2 = 0; int i3 = 0; ////////////////// String url = null; /* HTTPConnection httpConnection = null; HTTPResponse resp = null; */ BufferedReader reader = null; StringBuffer returnData = null; public void resetParameters()throws Throwable{ parameters = null; } public void addParameter(String name, String value)throws Throwable{ if (parameters == null){ parameters = new Hashtable(); } parameters.put(name,value); } public void resetCookies()throws Throwable{ cookies = null; } public void addCookie(String name, String value)throws Throwable{ if (cookies == null){ cookies = new Hashtable(); } cookies.put(name,value); } public void connect()throws Throwable{ httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); if (protocol.equals("https")){ authSSLProtocolSocketFactory = new AuthSSLProtocolSocketFactory(); authSSLProtocolSocketFactory.keyStoreName = keyStore; authSSLProtocolSocketFactory.keyStorePassword = keyStorePassword; authSSLProtocolSocketFactory.trustStoreName = trustStore; authSSLProtocolSocketFactory.trustStorePassword = trustStorePassword; myProtocol = new Protocol(protocol, authSSLProtocolSocketFactory, port); httpClient.getHostConfiguration().setHost(host, port, myProtocol); }else{ httpClient.getHostConfiguration().setHost(host, port, protocol); } HttpState initialState = new HttpState(); if (cookies != null){ for (Enumeration e = cookies.keys(); e.hasMoreElements();){ s1 = (String) e.nextElement(); cookie = new Cookie(host, s1, (String) cookies.get(s1), "/", -1, false); initialState.addCookie(cookie); } } if (httpAuthUsername != null){ httpClient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(httpAuthUsername, httpAuthPassword); initialState.setCredentials(new AuthScope(host, port, httpAuthRealm), defaultcreds); } httpClient.setState(initialState); } public void get()throws Throwable{ get(path); } public void get(String path)throws Throwable{ if (parameters == null){ nameValuePairs = null; }else{ nameValuePairs = new NameValuePair[parameters.size()]; i1 = -1; for (Enumeration e = parameters.keys(); e.hasMoreElements();){ i1 ++; s1 = (String) e.nextElement(); nameValuePairs[i1] = new NameValuePair(s1,(String) parameters.get(s1)); } } get(path,nameValuePairs); } public int get(String path, NameValuePair[] nameValuePairs)throws Throwable{ int i = 0; url = "/" + path; GetMethod method = new GetMethod(url); method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); if (nameValuePairs != null){ method.setQueryString(nameValuePairs); } // Execute the method. responseAsStream = new ByteArrayOutputStream(); int statusCode = httpClient.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { byte[] buffer = new byte[1000]; int bytesRead = 0; InputStream in = method.getResponseBodyAsStream(); while(true){ bytesRead = in.read(buffer); if (bytesRead == -1){ break; } responseAsStream.write(buffer,0,bytesRead); } responseAsStream.close(); in.close(); } method.releaseConnection(); return statusCode; } public void post()throws Throwable{ post(path); } public void post(String path)throws Throwable{ if (parameters == null){ nameValuePairs = null; }else{ nameValuePairs = new NameValuePair[parameters.size()]; i1 = -1; for (Enumeration e = parameters.keys(); e.hasMoreElements();){ i1 ++; s1 = (String) e.nextElement(); nameValuePairs[i1] = new NameValuePair(s1,(String) parameters.get(s1)); } } post(path,nameValuePairs); } public int post(String path, NameValuePair[] nameValuePairs)throws Throwable{ int i = 0; url = "/" + path; PostMethod method = new PostMethod(url); method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); if (nameValuePairs != null){ method.setRequestBody(nameValuePairs); } // Execute the method. responseAsStream = new ByteArrayOutputStream(); int statusCode = httpClient.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { byte[] buffer = new byte[1000]; int bytesRead = 0; InputStream in = method.getResponseBodyAsStream(); while(true){ bytesRead = in.read(buffer); if (bytesRead == -1){ break; } responseAsStream.write(buffer,0,bytesRead); } responseAsStream.close(); in.close(); } method.releaseConnection(); return statusCode; } public String getResponseAsString()throws Exception{ return responseAsStream.toString(); } public byte[] getResponseAsBytes()throws Exception{ return responseAsStream.toByteArray(); } } -----Original Message----- From: maikeru8 [mailto:msantos.my...@gmail.com] Sent: Wednesday, March 31, 2010 1:38 PM To: user@commons.apache.org Subject: how to implement login using httpclient? I'm still a beginner with regards to Apache Commons, and I wish to implement the login for a plugin in Eclipse. The user does not log in via the browser but rather Eclipse instead. And I found out about httpclient and I was wondering if I could use it to implement the client-side, and then I decided to use Java Servlets to implement the server-side. Any suggestions on how to use it? Just giving simple examples is good enough for me :) Thanks for those who will reply. :) -- View this message in context: http://n4.nabble.com/how-to-implement-login-using-httpclient-tp1747091p17470 91.html Sent from the Commons - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@commons.apache.org For additional commands, e-mail: user-h...@commons.apache.org