POST请求提供FileNotFoundException

时间:2014-08-23 16:40:15

标签: android http-post httpurlconnection filenotfoundexception http-status-code-400

POST请求为URL提供了FileNotFoundException

我正在向 android 中的网址 https://nitos.gr 发出Http POST请求。

网址没有域名。 Stackoverflow不允许我在我的文本中包含带有数字的URL,因此我只想写一个随机网址https://nitos.gr来获取示例。

我得到 400 响应代码,并打印getErrorStream(),提供libcore.net.http.UnknownLengthHttpInputStream@41eba330

但是,我成功执行了HTTP GET请求。网址为 https ,因此我有一个假信任管理器,允许所有SSL连接。

总结:

协议: HTTPS

获取请求成功

POST请求失败

请求代码: 400

错误消息: java.io.FileNotFoundException:https://nitos.gr

ErrorStream: java.io.FileNotFoundException:https://nitos.gr

执行POST请求的方法如下:

public void setTestbedData(String path, String data) {
        HttpURLConnection con = null;

        try {
            con = (HttpURLConnection) ( new URL(Constants.BASE_URL + path)).openConnection();
             // If you invoke the method setDoOutput(true) on the URLConnection, it will always use the POST method.
            con.setRequestMethod("POST");
            con.setDoInput(true);
            con.setDoOutput(true);

            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Content-Type", "application/json");

            Log.i("data", data);    

            OutputStream outputStream = con.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();

            Log.w("RESPONSE CODE", "code " + con.getResponseCode());

            Log.w("this is connection ",""+con); 
            InputStream errorstream = con.getErrorStream();             
            Log.w("GetErrorStream  ", ""+errorstream);

            InputStream _is;
            if (con.getResponseCode() >= 400) {  
                _is = con.getInputStream();  
            } else {  

                _is = con.getErrorStream();  

                String result = getStringFromInputStream(_is);
                Log.i("Error != 400", result);
            }


            if (con.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                    + con.getResponseCode());
            }



            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((con.getInputStream())));

            String output;
            Log.i("TestbedHttpClient","Output from Server:");
            while ((output = responseBuffer.readLine()) != null) {
                Log.i("Output",output);
            }

            con.disconnect();

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

错误消息:

响应代码(18936):代码400 libcore.net.http.HttpsURLConnectionImpl $ HttpUrlConnectionDelegate:https://nitos.gr GetErrorStream(18936): libcore.net.http.UnknownLengthHttpInputStream@41eba330 System.err(18936): java.io.FileNotFoundException: https://nitos.gr  System.err(18936):at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)

2 个答案:

答案 0 :(得分:2)

替换以下代码

        Log.w("this is connection ",""+con); 
        InputStream errorstream = con.getErrorStream();             
        Log.w("GetErrorStream  ", ""+errorstream);

        InputStream _is;
        if (con.getResponseCode() >= 400) {  
            _is = con.getInputStream();  
        } else {  

            _is = con.getErrorStream();  

            String result = getStringFromInputStream(_is);
            Log.i("Error != 400", result);
        }

        InputStream _is;
        if (con.getResponseCode() / 100 == 2) { // 2xx code means success
            _is = con.getInputStream();  
        } else {  

            _is = con.getErrorStream();  

            String result = getStringFromInputStream(_is);
            Log.i("Error != 2xx", result);
        }

或者换句话说:当HTTP状态代码大于或等于400时,从getInputStream()读取是没有意义的。它不是那么简单,也许你必须处理一些3xx代码。看看List of HTTP status codes

最后一句话:如果您发现HttpURLConnection使用起来很麻烦,您可以使用列出的here之一的抽象库。

答案 1 :(得分:1)

最后,由于服务器具有身份验证,我无法执行POST请求。 因此,POST请求的配置没有问题。 因此,现在我应该找到一种方法将证书附加到服务器的REST调用。