0

赤い酸素サーバーを介してSMSを送信しようとしているコードは次のとおりです

これが私が最終的に実行しているコードです 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

次のコードは、他の Web サイトでテストしたところ問題なく動作しました (メッセージング 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 の命名規則やベスト プラクティスを認識していないようです。かなり整理しました。
重複を避けるために、ストリーム リーダーを取り出して別のメソッドにすることができます。
別の Web サイト (私は自分の作品を使用しました) を指定して、出力が得られるかどうかを確認することをお勧めします。

于 2013-02-19T11:28:58.823 に答える
0

これは、ネットワーク トポロジの問題です。現在いる場所からそのサイトに接続できません。おそらくあなた自身のものである、介在するファイアウォールがそれを妨げています。ネット管理者に相談してください。

于 2013-02-19T22:44:28.317 に答える