PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
HTTP_CLIENT usage info

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTTP
View previous topic :: View next topic  
Author Message
saahmad



Joined: 11 May 2007
Posts: 1

PostPosted: Fri May 11, 2007 11:10 am    Post subject: HTTP_CLIENT usage info Reply with quote

Hi
I am am new user to pear applications.

I am designing a Single Sign On application and require that i login into two websites simultaneously from one login screen and then redirect to another page

I tried the HTTP_Request but it hangs as there is a ":" in the POST URL of one site.

So any help on how to use this new HTTP_CLIENT class. :?
Back to top
View user's profile Send private message
willsmith12



Joined: 19 Sep 2011
Posts: 4

PostPosted: Mon Sep 19, 2011 2:36 pm    Post subject: Reply with quote

Hi...The following code is a demo of how to use httpclient. With HttpClientHelper, you can easily download html,image,flash,pdf ect. HttpClientHelper also helps you do some form submit.

CODE:
package base.helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


public class HttpClientHelper {
private HttpClient client = null;

private String baseUrl;

private String encoder;

public HttpClientHelper(String baseUrl, String encoder) {

createClient();
this.baseUrl = baseUrl;
this.encoder = encoder;
}

private void createClient() {
System.getProperties().setProperty("httpclient.useragent",
"Mozilla/4.0");
client = new HttpClient(new MultiThreadedHttpConnectionManager());

}

/**
* Download the url as html String
*
* @param url
* the url to be download.
* @return
*/
public String html(String url) {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
GetMethod get = new GetMethod(url);
get.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
get.setFollowRedirects(true);

try {
client.executeMethod(get);
return MiscHelper.method(get, this.encoder);

} catch (Exception e) {
createClient();
e.printStackTrace();
} finally {
get.releaseConnection();
}
return null;
}

/**
* Download the url conent and save into the file. It can use to download
* images,pdf,flash etc.
*
* @param url
* @param file
* @return
*/
public boolean file(String url, File file) {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
GetMethod get = new GetMethod(url);
get.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

get.setFollowRedirects(true);
boolean result = true;
try {
client.executeMethod(get);
InputStream in = get.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int count = -1;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
createClient();
result = false;
} finally {
get.releaseConnection();
}
return result;
}

public boolean file(String url) {
int i = url.lastIndexOf('/');
String fileName = url.substring(i + 1);
return file(url, new File(fileName));
}
/**
* Submit the form to the url.
* @param url
* @param params
* @return
* @throws HttpException
* @throws IOException
*/
public String postForm(String url, List params)
throws HttpException, IOException {
url = MiscHelper.toAbsoluteUrl(this.baseUrl, url);
PostMethod authpost = new UTF8PostMethod(url);
authpost.setRequestHeader("ContentType",
"application/x-www-form-urlencoded;charset=UTF-8");
// authpost.addRequestHeader("Content-Type","Content-Type: text/html;
// charset=utf-8");
// Prepare login parameters
NameValuePair[] postParams = new NameValuePair[params.size()];
params.toArray(postParams);
authpost.setRequestBody(postParams);
client.executeMethod(authpost);
authpost.releaseConnection();
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
return newuri;
}
}
return null;

}

public static class UTF8PostMethod extends PostMethod {
public UTF8PostMethod(String url) {
super(url);
}

@Override
public String getRequestCharSet() {
// return super.getRequestCharSet();
return "UTF-8";
}
}

}


software testing services in india
_________________
software testing services
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTTP All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement