带有Urlconnection获取请求的FileNotFoundException

时间:2015-11-18 05:21:04

标签: java android httpurlconnection urlconnection httpsurlconnection

使用GET执行URLConnection请求。代码在这里

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

获取java.io.FileNotFoundException而请求的网址是正确的。我认为这可能是https ssl证书问题。如果有其他人得到这个问题并解决请更新。

1 个答案:

答案 0 :(得分:2)

  

对参数进行编码以创建request.Unsupported的URL   参数值中的字符可能会导致异常,也可能是空格。

    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