简体   繁体   中英

FileNotFoundException with Urlconnection Get Request

Doing GET request with URLConnection . code is here

java.net.URL url = new java.net.URL(requestUrl);
        URLConnection urlConnection = url.openConnection();
        is = new BufferedInputStream(urlConnection.getInputStream());

getting java.io.FileNotFoundException whereas requested url is correct. i think it may be https ssl certificate issue. if anyone else got this issue and resolved please update.

Encode your parameter to create an URL for request.Unsupported character in parameter value may cause to exceptions it can be a white space also.

    String url = "http://url.com";
    String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
    String param1 = "value1";
    String param2 = "value2";
    // ...

    String query = String.format("param1=%s&param2=%s", 
         URLEncoder.encode(param1, charset), 
         URLEncoder.encode(param2, charset));

    URLConnection connection = new URL(url + "?" + query).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);
    InputStream response = connection.getInputStream();
// ...

Courtsey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM