java.io.FileNotFoundException : while accessing REST service 错误

标签 java web-services rest 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);       
    }
}

日志:

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)

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

最佳答案

  1. 它实际上是一个 HttpsURLConnection(你打开的是一个 https://url)。

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

关于java.io.FileNotFoundException : while accessing REST service 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29075680/

相关文章:

java - 大括号在转义 SQL 语句中被删除

java - 使用 android 进行 Ant emma 测试 "Could not find file coverage.em to delete."

java - 如何从 REST 服务生成实体?

java - Axis 2 中 SOAP 消息的加密

java - JAX-RS jersey ExceptionMappers 用户定义的异常

java - 我如何解密和加密 Java 程序的 Joomla 用户密码?

java - 使用java美化json文件

c# - 自定义日期时间 XML 序列化

ruby-on-rails - 如何使用 Ruby on Rails 为 REST API 生成自定义响应?

mysql - 测试环境的 Quartz 调度程序不工作