2

I have a requirement where i have to send sms from PC to mobile. The code which i have implemented works perfectly on my machine at home but it fails and throws Exception when i run the code in my organization. I get Exception java.net.ConnectException: Connection timed out: connect And the exception caused is on this line

 HttpResponse response = this.httpclient.execute(httpost);

. I am able to access the site, but through code it throws exception. Please find the below code which i have implemented.

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class MobileTry 
{
private final String LOGIN_URL    = "http://fullonsms.com/home.php";
private final String SEND_SMS_URL = "http://fullonsms.com/home.php";
private final String LOGOUT_URL = "http://fullonsms.com/logout.php?LogOut=1";

private final int MESSAGE_LENGTH = 10;
private final int MOBILE_NUMBER_LENGTH = 140;
private final int PASSWORD_LENGTH = 10;

private String mobileNo;
private String password;
private DefaultHttpClient httpclient;
MobileTry(String username,String password)
{
 this.mobileNo = username;
 this.password = password;
 httpclient = new DefaultHttpClient();
}



public boolean isLoggedIn() throws IOException {
 // User Credentials on Login page are sent using POST
 // So create httpost object
 HttpPost httpost = new HttpPost(LOGIN_URL);

 // Add post variables to login url
 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
 nvps.add(new BasicNameValuePair("MobileNoLogin", mobileNo));
 nvps.add(new BasicNameValuePair("LoginPassword", password));
 httpost.setEntity(new UrlEncodedFormEntity(nvps));

 // Execute request
 try{
 HttpResponse response = this.httpclient.execute(httpost);
 HttpEntity entity = response.getEntity();
 if (entity != null) {
     System.out.println("entity " + slurp(entity.getContent(), 10000000));
     System.out.println("entity " + response.getStatusLine().getStatusCode());

  return true;
 }
 }
 catch(Exception e)
 {
     e.printStackTrace();
     System.out.println(e);
 }
 //Check response entity
return false;

}

public boolean sendSMS(String toMobile,String message) throws IOException {
 HttpPost httpost = new HttpPost(SEND_SMS_URL);
 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
 nvps.add(new BasicNameValuePair("MobileNos", toMobile));
nvps.add(new BasicNameValuePair("Message", message));

httpost.setEntity(new UrlEncodedFormEntity(nvps));
 HttpResponse response = this.httpclient.execute(httpost);
 HttpEntity entity = response.getEntity();
 if(entity != null) {
                       System.out.println("entity " + slurp(entity.getContent(), 10000000));
                       System.out.println("entity " + response.getStatusLine().getStatusCode());
  return true;
 }
 return false;
}

public boolean logoutSMS() throws IOException {
 HttpGet httpGet = new HttpGet(LOGOUT_URL);
 HttpResponse response;
 response = this.httpclient.execute(httpGet);
 HttpEntity entity = response.getEntity();
 if (entity != null) {
  System.out
    .println("entity " + slurp(entity.getContent(), 10000000));
  System.out.println("entity "
    + response.getStatusLine().getStatusCode());
  return true;
 }
 return false;
}


public static String slurp(final InputStream is, final int bufferSize)
{
  final char[] buffer = new char[bufferSize];
  final StringBuilder out = new StringBuilder();
  try {
    final Reader in = new InputStreamReader(is, "UTF-8");
    try {
      for (;;) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0)
          break;
        out.append(buffer, 0, rsz);
      }
    }
    finally {
      in.close();
    }
  }
  catch (UnsupportedEncodingException ex) {
    /* ... */
  }
  catch (IOException ex) {
      /* ... */
  }
  return out.toString();
}

/**
 * @param args
 */
public static void main(String[] args) {
 String username = "98XXXXX";
 String password = "XXXXX";
 String toMobile = "97XXXXX";

 String toMessage = "Message Sent";

 MobileTry fullOnSMS = new MobileTry(username, password);
 try{
  if(fullOnSMS.isLoggedIn() && fullOnSMS.sendSMS(toMobile,toMessage))
  {
   fullOnSMS.logoutSMS();
   System.out.println("Message was sent successfully " );
  }
  System.exit(0);
 }

 catch(IOException e)
 {
     System.out.println(e);
     System.out.println("Unable to send message, possible cause: " + e.getMessage());
 }
}
}

The Exception which i am getting is as below

java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at com.cris.lms.trialmonitoring.action.UnusualMobileTry.isLoggedIn(MobileTry.java:54)
    at com.cris.lms.trialmonitoring.action.UnusualMobileTry.main(MobileTry.java:148)
java.net.ConnectException: Connection timed out: connect

Any help will be much appreciated.

sTg
  • 4,313
  • 16
  • 68
  • 115
  • Have you made any attempt at troubleshooting? Used Fiddler, Firebug and/or Wireshark? Tell us what you have done so far. – Jim Garrison Oct 16 '13 at 07:43
  • Firewall problem? If the code works on your home, maybe your company blocks by firewall these requests... – Gerard Ribas Oct 16 '13 at 07:47
  • @ Jim : I am creating a POC for now.. I am running the code through main method of class file where it is throwing excpetion. – sTg Oct 16 '13 at 07:51
  • @gerardibas : One more thing i just observed..I am not able to access any site through java code.Not even google, For every site it is throwing the same exception java.net.ConnectException...I am clueless...I dont think organizations block google..There is something i have missed. – sTg Oct 16 '13 at 07:58
  • If you cannot reach any site from Java, can you reach any with an ordinary browser? If so, can you figure out, if there is some fancy proxy setup or something? – Fildor Oct 16 '13 at 08:13

0 Answers0