= 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