簡體   English   中英

在HttpURLConnection的情況下如何解決FileNotFoundException

[英]How to resolve FileNotFoundException incase HttpURLConnection

我有一個應用程序,該應用程序通過POST方法發送短信,該方法工作正常,但有一段時間會出現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)

您在讀取輸入流之前先斷開連接。 移動以下代碼行:

connection.disconnect();

在閱讀響應流之后的一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM