Java Socket连接超时错误联系SMS网关时出现错误

时间:2013-02-19 11:04:35

标签: java http sockets sms

以下是我尝试通过红色氧气服务器发送短信的代码

以下是我正在执行的代码 final String requestURL =“http://www.redoxygen.net/sms.dll?Action=SendSMS”;

    final StringBuilder stringBuilder = new StringBuilder();
    try {
        stringBuilder.append("AccountId=").append(URLEncoder.encode("****", "UTF-8"))
                .append("&Email=").append(URLEncoder.encode("*******", "UTF-8"))
                .append("&Password=").append(URLEncoder.encode("******", "UTF-8"))
                .append("&Recipient=").append(URLEncoder.encode("******", "UTF-8"))
                .append("&Message=").append(URLEncoder.encode("hello", "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    final URL address;
    try {
        address = new URL(requestURL);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    final HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) address.openConnection();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        connection.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new RuntimeException(e);
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setConnectTimeout(100000000);

    DataOutputStream output = null;
    try {
        output = new DataOutputStream(connection.getOutputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        output.writeBytes(stringBuilder.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

执行时我得到以下异常:

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:904)
at com.nextenders.server.LoginServlet.SendSMS(LoginServlet.java:143)

我尝试增加连接timeout并关闭防火墙等等但没有运气。任何人都可以帮我跟踪问题?

这是我正在遵循的教程: http://www.redoxygen.com/developers/java/

我的代码中的“ * ”是网关的凭据。

2 个答案:

答案 0 :(得分:0)

以下代码在我与其他网站进行测试后运行正常(因为我没有访问您的消息传递API):

    final String requestURL = "http://www.redoxygen.net/sms.dll?Action=SendSMS";
    final StringBuilder stringBuilder = new StringBuilder();
    try {
        stringBuilder.append("AccountId=").append(URLEncoder.encode("****", "UTF-8"))
                .append("&Email=").append(URLEncoder.encode("*******", "UTF-8"))
                .append("&Password=").append(URLEncoder.encode("******", "UTF-8"))
                .append("&Recipient=").append(URLEncoder.encode("******", "UTF-8"))
                .append("&Message=").append(URLEncoder.encode("hello", "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    final URL address;
    try {
        address = new URL(requestURL);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    final HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) address.openConnection();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        connection.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new RuntimeException(e);
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setConnectTimeout(100000000);

    try {
        connection.connect();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    final DataOutputStream output;
    try {
        output = new DataOutputStream(connection.getOutputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        output.writeUTF(stringBuilder.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    final InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException e) {
        final char[] buffer = new char[0x10000];
        final StringBuilder stackBuilder = new StringBuilder();
        final Reader in;
        try {
            in = new InputStreamReader(connection.getErrorStream(), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
        try {
            int read;
            do {
                read = in.read(buffer, 0, buffer.length);
                if (read > 0) {
                    stackBuilder.append(buffer, 0, read);
                }
            } while (read >= 0);
            System.out.println("Error response code from server. Error was:");
            System.out.println(stackBuilder.toString());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        throw new RuntimeException(e);
    }

    final char[] buffer = new char[0x10000];
    final StringBuilder stackBuilder = new StringBuilder();
    final Reader in;
    try {
        in = new InputStreamReader(inputStream, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex);
    }
    try {
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                stackBuilder.append(buffer, 0, read);
            }
        } while (read >= 0);
        System.out.println(stackBuilder.toString());
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

它将toString各种流,并在流未连接时进行错误处理 最重要的更改是使用DataOutputStream.writeUTF方法而不仅仅使用write方法 - 这将确保正确编码POST数据。我不认为这是你的问题,问题出在connect上 您使用的示例代码似乎没有意识到Java命名约定或最佳实践,我已经大大整理了它 可以将流读取器拉出并分成单独的方法以避免重复 我建议把它指向另一个网站(我用过我的作品)并看看你是否得到输出。

答案 1 :(得分:0)

这是一个网络拓扑问题。您无法从您所在的位置连接到该网站。一些干预防火墙,可能是你自己的,正在阻止它。和你的netadmin交谈。