I have a simple code to access twitter API using twitter4j library. The code is working fine when I run it independently. But when I deploy the same code on weblogic server as part of a bigger application, it starts giving me following error.
java.net.ConnectException: Tried all: 4 addresses, but could not connect over HTTPS to server: api.twitter.com port: 443
The catch here is that the we need to use proxy server for accessing internet. I have given the proxy when connecting to the twitter API and it seems to be working when running independently. But same is not happening after deploying the appliation on weblogic. The code for connecting to twitter API is below
public static String getTwitterFeed() {
String retVal = "";
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (Exception e) { // should never happen
e.printStackTrace();
}
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXXXXX").
setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXX").
setOAuthAccessToken("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx").
setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").
setHttpProxyHost("proxy.com").
setHttpProxyPort(1111);
try {
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
AppModule am = new AppModule();
String tag = am.getTwitterTags();
if(tag == null || tag.length()==0) {
System.out.println("no tag found");
tag = "#rajneesh";
}
Query query = new Query(tag);
query.setCount(5);
QueryResult result;
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
String tweetText = twitterTemplate;
tweetText = tweetText.replaceAll("~author~", tweet.getUser().getScreenName()).replaceAll("~Tweet",tweet.getText()).
replaceAll("~elapsed time~", tweet.getCreatedAt().toString());
retVal = retVal+tweetText;
}
} catch (TwitterException te) {
te.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String html = "<html><head></head><body>" + retVal+"</body></html>";
// System.out.println(html);
return html;
}