- Create a new class implemented the CredentialSupplier interface. this class stores the username and password for the basic authentication and returns the Credentials object of the apache http client. following code is a sample class implemented the CredentialSupplier interface
import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.CredentialSupplier;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.Credentials;
public class AuthCredentialSupplier implements CredentialSupplier{
private String username=null;
public void setUsername(String username){
this.username=username;
}
public String getUsername(){
return this.username;
}
private String password=null;
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return this.password;
}
public AuthCredentialSupplier(){
}
public AuthCredentialSupplier(String username,String password){
setUsername(username);
setPassword(password);
}
public Credentials getCredentials(String realm, String host){
String username=getUsername();
String password=getPassword();
return new UsernamePasswordCredentials(username,password);
}
} - Access to the rss url and get rss data from the basic authenticated web site. When accessing to the rss url, we need to use the HttpClientFeedFetcher instead of HttpURLFeedFetcher.
//sample program
//rss url
String url="http://twitter.com/statuses/friends_timeline.atom";
//username and password
String username="username";
String password="password";
try{
//create and initialize CredentialSupplier Object
AuthCredentialSupplier authCredentials=new AuthCredentialSupplier(username,password);
//create HttpClientFeedFetcher object
//(we can not use HttpURLFeedFetcher with basic authentication)
FeedFetcher feedFetcher=new HttpClientFeedFetcher(null,authCredentials);
List result=(feedFetcher).retrieveFeed(new URL(url)).getEntries();
//get response.
if(result!=null){
.....
}
else{
System.out.println("ERROR")
}
}catch(FetcherException e){
int responseCode=e.getResponseCode();
System.out.println("ERROR, response code="+responseCode+", error="+e);
}catch(Exception e){
System.out.println("Unexpected Exception, e="+e)
}
2009年4月28日火曜日
[rome-fetcher]how to get rss data from the basic authenticated web site
Steps to get rss data from the basic authenticated web site like the twitter.
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿