java - 带有 POST 和不可变 doOutput 变量的 HttpsURLConnection 上的 FileNotFoundException

标签 java android httpsurlconnection

我正在尝试将一些数据发布到我的 Android 应用程序中的 https url,以获得 json 格式响应。

我面临两个问题:

is = conn.getInputStream();

抛出

java.io.FileNotFoundException

我不知道我是否对 HttpsURLConnection 做错了什么。

第二个问题出现在我调试代码时(使用eclipse);我在之后设置了断点

conn.setDoOutput(true);

并且,在检查 conn 时值,我看到变量 doOutput保持设置为false并输入 GET .

<小时/>

我的 https POST 方法如下,其中 POSTData是一个扩展 ArrayList<NameValuePair> 的类

private static String httpsPOST(String urlString, POSTData postData,  List<HttpCookie> cookies) {

    String result = null;
    HttpsURLConnection conn = null;
    OutputStream os = null;
    InputStream is = null;

    try {
        URL url = new URL(urlString);
        conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setUseCaches (false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        if(cookies != null)
            conn.setRequestProperty("Cookie",
                    TextUtils.join(";", cookies));

        os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(postData.getPostData());
        writer.flush();
        writer.close();

        is = conn.getInputStream();
        BufferedReader r = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder total = new StringBuilder(); 
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        result = total.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        if (conn != null) {
            conn.disconnect();
        }
    }

    return result;
}
<小时/>

一个小更新:显然 eclipse debug 对我撒了谎,在 netbeans 上运行和调试显示 POST联系。错误似乎与我传递给网址的参数有关。

最佳答案

FileNotFoundException 表示您发布的 URL 不存在,或者无法映射到 servlet。它是 HTTP 404 状态代码的结果。

如果调试器中看到的内容与程序的行为方式不一致,请不要担心。如果 doOutput 确实没有启用,您将在获取输出流时遇到异常。

关于java - 带有 POST 和不可变 doOutput 变量的 HttpsURLConnection 上的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21646961/

相关文章:

android - Termux 上的 Cython。导入错误: dynamic module does not define module export function (PyInit_libmc)

java - 使用 FIddler 捕获 Android 应用程序流量

java - HttpsURLConnection getHeaderFields 不返回 set-cookie

java - 如何获取 Google 返回的 JSON 对象中的持续时间和距离?

java - 在 Java 中使用 if 语句检查类

android - 如何将 Android ListView 放置在 View 上方 - 不占用全部空间

Java - 使用 SSH 隧道连接到网页

java - JAX-WS 客户端的 HTTP 基本身份验证似乎失败

java - getLocalAddress() 返回 0.0.0.0

android - 从 jni 返回无符号字符 []