java.lang.StringIndexOutOfBoundsException使用SSL创建URL请求时

时间:2019-05-10 10:36:13

标签: java

我拥有这样的宁静的代码,可以抛出java.lang.StringIndexOutOfBoundsException: String index out of range: -1

    URL url = new URL(serverURL);

    // open a connection to that source
    URLConnection urlConnection = url.openConnection();

    HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){

                public boolean verify(String hostname,
                        javax.net.ssl.SSLSession sslSession) {
                        return true;
                }
            });
    HttpsURLConnection con = (HttpsURLConnection) urlConnection;
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { new SffX509TrustManager(TQLCLI.localConfig) }, new SecureRandom());
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    con.setSSLSocketFactory(sslSocketFactory);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { new SffX509TrustManager(TQLCLI.localConfig) }, new SecureRandom());
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        con.setSSLSocketFactory(sslSocketFactory);


    // Exception throws somewhere in the following line

    con.setRequestMethod("POST");
    con.setDoOutput(true);
    OutputStream outStream = con.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(outStream);
    osw.write(postBody);
    osw.flush();
    osw.close();
    outStream.close();
    con.connect();

    BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    con.disconnect();
    // System.out.println(result.toString());
    return result.toString();

在第outStream = con.getOutputStream();行,其抛出java.lang.StringIndexOutOfBoundsException

您在此代码段中是否看到任何会引起StringIndexOutOfBoundsException的问题?

0 个答案:

没有答案