Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 96100 invoked from network); 31 Mar 2010 18:00:54 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 31 Mar 2010 18:00:54 -0000 Received: (qmail 7513 invoked by uid 500); 31 Mar 2010 18:00:52 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 7388 invoked by uid 500); 31 Mar 2010 18:00:52 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 7380 invoked by uid 99); 31 Mar 2010 18:00:52 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 Mar 2010 18:00:52 +0000 X-ASF-Spam-Status: No, hits=-1.0 required=10.0 tests=AWL,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [74.208.4.194] (HELO mout.perfora.net) (74.208.4.194) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 Mar 2010 18:00:46 +0000 Received: from steve (rrcs-96-10-246-41.midsouth.biz.rr.com [96.10.246.41]) by mrelay.perfora.net (node=mrus3) with ESMTP (Nemesis) id 0LaGNq-1NHfmE3aoQ-00m5pJ; Wed, 31 Mar 2010 14:00:22 -0400 From: "Steve Cole" To: "'Commons Users List'" References: <1270057064420-1747091.post@n4.nabble.com> In-Reply-To: <1270057064420-1747091.post@n4.nabble.com> Subject: RE: how to implement login using httpclient? Date: Wed, 31 Mar 2010 14:00:19 -0400 Message-ID: <4AB481618BDC48769908C6656509F7CD@cbs> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AcrQ+O9gFCXyJnPnTiumyu0a06nzwAAAolUg X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7600.16385 X-Provags-ID: V01U2FsdGVkX1/70D2BS0adZJSXm20+8RvesyO3h6pXF68t1Np 78ciCVQdxV4qOa1UE9skOskp60Ee+s5WBxVGrNeOCzRGTLd5kj xBdPtVk0tidtMnu1aCwfJV3mwAW+SDS 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.mykel@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-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org