1

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;
 }
Tim
  • 41,901
  • 18
  • 127
  • 145
user1996183
  • 121
  • 1
  • 2
  • 7
  • If you want to use the same proxy for all outbound requests, you can use -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=1111. If you want to exclude certain hosts, you can use -Dhttp.nonProxyHosts="*.somedomain.com|localhost" . This will eliminate the need to set proxy details in code.You can set this in setDomainenv. – coderplus Jun 07 '14 at 16:04
  • Hi @Annesh Joseph. I tried the steps you suggested. Now I am getting the following error Failed to communicate with proxy: proxy.com/XXXX. Will try connection api.twitter.com/443 now. weblogic.net.http.HttpUnauthorizedException: Proxy or Server Authentication Required at weblogic.net.http.HttpURLConnection.getAuthInfo(HttpURLConnection.java:297) Truncated. see log file for complete stacktrace We dont' have any authentication for the proxy. Any idea what's wrong here? – user1996183 Jun 18 '14 at 04:13
  • Are you sure you don't need authentication for your proxy? Sometimes proxy authentication within our PC are done behind the scene using authentication data we provided when logging in to the office's domain. If you want to try setting user/password for proxy authentication, refer: http://stackoverflow.com/questions/13152861/system-proxy-setting-java – aff Jun 03 '15 at 08:01

0 Answers0