java.io.FileNotFoundException:访问 REST 服务时

Nan*_*ani 5 java rest web-services filenotfoundexception

我尝试通过 REST API post 方法访问 Web 服务,但最终出现 FileNotFoundException

代码:

public class TestService {

    static {
        disableSSLVerification();
    }

    public static void main(String[] args) {

        String[] names = {"login","seq","password"};    
        String[] values = { "admin", "2811", "admin" }; 

        String url = "https://localhost:8844/login";

        try {
            httpPost(url, names, values);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        OutputStream out = conn.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        for (int i = 0; i < paramName.length; i++) {
            writer.write(paramName[i]);
            writer.write("=");
            writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
            writer.write("&");
        }
        System.out.println("WRITER: " + writer);
        writer.close();
        out.close();

        if (conn.getResponseCode() != 200) {
            throw new IOException(conn.getResponseMessage());
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();

        conn.disconnect();
        return sb.toString();
    }

    public static void disableSSLVerification() {

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }

        } };

        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };      
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);       
    }
}
Run Code Online (Sandbox Code Playgroud)

日志:

java.io.FileNotFoundException: https://localhost:8844/login
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown S
ource)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unkn
own Source)
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题吗?请尝试帮助我将其标记为“重复”。

Umb*_*ndi 3

  1. 它实际上是一个 HttpsURLConnection(您正在打开一个 https:// url)。

  2. 该 URL 不存在,请尝试在浏览器中打开它。如果 url 存在,则可能是您在该 https 主机上使用自签名证书,该证书被 java urlconnection 类拒绝(但我不认为是这种情况,例外应该有所不同,在这种情况下您'无论如何,我们都需要实现一个接受证书的包装器)。