0

这是我试图通过红色氧气服务器发送短信的代码

这是我在 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/

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

4

2 回答 2

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 命名约定或最佳实践,我已经对其进行了相当大的整理。
可以将流阅读器拉出并放入单独的方法中以避免重复。
我建议将它指向另一个网站(我使用了我的作品),看看你是否得到输出。

于 2013-02-19T11:28:58.823 回答
0

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

于 2013-02-19T22:44:28.317 回答