5

I'm trying to access a web service through REST API post method and end up with FileNotFoundException

code:

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);       
    }
}

Log:

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)

can anyone please help me to resolve this? please try to help me rather marking this one 'duplicate'.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Nani
  • 1,148
  • 3
  • 20
  • 35

3 Answers3

3
  1. It's actually an HttpsURLConnection (you are opening a https:// url).

  2. That URL does not exist, try opening it in your browser. If the url exists it could be that you are using a self-signed certificate on that https host, that is rejected by java urlconnection classes(but i don't think that's the case, the exception should be different, in that case you'll need to implement a wrapper that accept the certificate anyway).

uraimo
  • 19,081
  • 8
  • 48
  • 55
  • I have modified HttpsURLConnection and still same problem. I'm bypassing the ssl verification for just testing purpose. And the same code for bypassing and accessing web service is absolutely working for other web service endpoint but i'm have problem access the web service running in local machine. is there any way to access local machine's web service? – Nani Mar 17 '15 at 06:40
  • 1
    Correct, changing the variable to Https can't fix anything. Yes, you can access a ws on localhost. I suppose that you can open that https url in your browser,right? Does using 127.0.0.1 instead of localhost changes anything? – uraimo Mar 17 '15 at 07:37
2

You may try removing:

conn.setDoOutput(true);
conn.setDoInput(true);
Mukarram Ali
  • 387
  • 5
  • 24
0

since response was not received because there's the problem with the method signature. it has been updated. I have changed accordingly and now it is working fine.

String[] names = {"username","password"};    
String[] values = { "admin", "admin" }; 
String url = "https://localhost:8844/session";
Nani
  • 1,148
  • 3
  • 20
  • 35
  • How about explaining the problem. This answer doesn't help any future readers – Paul Samsotha Mar 17 '15 at 09:35
  • @peeskillet: it is useful because if there is any problem with the method signature then we will get FileNotFoundException – Nani Mar 17 '15 at 09:45
  • I meant explain what was wrong with the method signature. If you are going to answer your own question, then answer it like you would any one else's question. – Paul Samsotha Mar 17 '15 at 09:50