0

POST メソッドを介して SMS を送信するアプリケーションがあります。このメソッドは正常に動作しますが、FileNotFound Exception が発生することがあります。何が問題なのか教えてください。

以下は私のコードです。ありがとう

 public static String sendSMS(String data, String url1) {
            URL url;

            String status = "Somthing wrong ";
            try {
                url = new URL(url1); 
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setInstanceFollowRedirects(false); 
                connection.setRequestMethod("POST"); 
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
                connection.setRequestProperty("charset", "utf-8");
                connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length));
                connection.setUseCaches (false);

                DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
                wr.writeBytes(data);
                wr.flush();
                wr.close();
                connection.disconnect();

                // Get the response
                try {
                    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String s;
                    while ((s = rd.readLine()) != null) {
                        status = s;
                    }
                    rd.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }



            } catch (MalformedURLException e) {
                status = "MalformedURLException Exception in sendSMS";
                e.printStackTrace();
            } catch (IOException e) {
                status = "IO Exception in sendSMS";
                e.printStackTrace();
            }

            return status;
        }

エラー 私が得たもの

java.io.FileNotFoundException: http://.... で sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)

4

1 に答える 1

3

入力ストリームを読み取る前に接続を切断しました。次のコード行を移動します。

connection.disconnect();

応答ストリームを読んだのポイントまで。

于 2013-02-16T05:41:30.520 に答える