pawan jangir
pawan jangir

Reputation: 1

HttpsURLConnection POST method: java.net.ConnectException: Connection timed out: connect

I have been trying to use an API with HttpsURLConnection POST method which sends an SMS. The class works fine when I call the function from the main function of the same class. However when I call the function from another class or jsp, It throws the time out error. I have done a lot of searching on this issue but couldn't get a breakthrough.

Below is my class:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;

public class SMS_exp {

    public List<HttpCookie> cookies;
    public HttpsURLConnection conn;
    private final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {
        String msg = "Hello There. You get the SMS.";
        String mob = "<Mobile Number>";
        SMS_exp as = new SMS_exp();
        System.out.println(as.sendPostJson(msg, mob, ""));
    }

    public String sendPostJson(String msg, String mob, String postParams) throws Exception {
        String url = "https://server.sms.com/v4/?key&method=sms&message=" + msg + " &to=" + mob + "&sender=XXXXXX";
        StringBuffer response = new StringBuffer();
        //try {
            URL obj = new URL(url);
            conn = (HttpsURLConnection) obj.openConnection();
            conn.setUseCaches(false);
            System.setProperty("java.net.useSystemProxies", "true");
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Host", "https://server.sms.com/");
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8,hi;q=0.6");
            conn.addRequestProperty("Cookie", "");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Referer", "https://server.sms.com/");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
            conn.setDoOutput(true);
            conn.setDoInput(true);
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(postParams);
            wr.flush();
            wr.close();
            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url + " ");
            System.out.println("Post parameters : " + postParams);
            System.out.println("Response Code : " + responseCode);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getResponseCode() / 100 == 2 ? conn.getInputStream() : conn.getErrorStream(), "UTF-8"));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        /*} catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return response.toString();
    }
}

Below is the call method:

{SMS_exp ok = new SMS_exp();
    String pass1 = "123456", add = "1234567890";
    String msg = pass1 + " Hello There. You get the SMS.";
    String a = ok.sendPostJson(msg, add,"");
    System.out.print(a);}

Below is the error stack:

java.net.ConnectException: Connection timed out: connect
    java.net.DualStackPlainSocketImpl.connect0(Native Method)
    java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    java.net.PlainSocketImpl.connect(Unknown Source)
    java.net.SocksSocketImpl.connect(Unknown Source)
    java.net.Socket.connect(Unknown Source)
    sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    sun.net.NetworkClient.doConnect(Unknown Source)
    sun.net.www.http.HttpClient.openServer(Unknown Source)
    sun.net.www.http.HttpClient.openServer(Unknown Source)
    sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    SMS_exp.sendPostJson(SMS_exp.java:46)

Could you help me out, where its going wrong?

Upvotes: 0

Views: 145

Answers (0)

Related Questions