Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java url.openConnection()发出多个服务器请求_Java_Android_Http_Https - Fatal编程技术网

Java url.openConnection()发出多个服务器请求

Java url.openConnection()发出多个服务器请求,java,android,http,https,Java,Android,Http,Https,我用它来发出服务器请求 String message = URLEncoder.encode("my message", "UTF-8"); try { // instantiate the URL object with the target URL of the resource to // request URL url = new URL("http://www.example.com/comment"

我用它来发出服务器请求

String message = URLEncoder.encode("my message", "UTF-8");

try {
    // instantiate the URL object with the target URL of the resource to
    // request
    URL url = new URL("http://www.example.com/comment");

    // instantiate the HttpURLConnection with the URL object - A new
    // connection is opened every time by calling the openConnection
    // method of the protocol handler for this URL.
    // 1. This is the point where the connection is opened.
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    // set connection output to true
    connection.setDoOutput(true);
    // instead of a GET, we're going to send using method="POST"
    connection.setRequestMethod("POST");

    // instantiate OutputStreamWriter using the output stream, returned
    // from getOutputStream, that writes to this connection.
    // 2. This is the point where you'll know if the connection was
    // successfully established. If an I/O error occurs while creating
    // the output stream, you'll see an IOException.
    OutputStreamWriter writer = new OutputStreamWriter(
            connection.getOutputStream());

    // write data to the connection. This is data that you are sending
    // to the server
    // 3. No. Sending the data is conducted here. We established the
    // connection with getOutputStream
    writer.write("message=" + message);

    // Closes this output stream and releases any system resources
    // associated with this stream. At this point, we've sent all the
    // data. Only the outputStream is closed at this point, not the
    // actual connection
    writer.close();
    // if there is a response code AND that response code is 200 OK, do
    // stuff in the first if block
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        // OK

        // otherwise, if any other status code is returned, or no status
        // code is returned, do stuff in the else block
    } else {
        // Server returned HTTP error code.
    }
} catch (MalformedURLException e) {
    // ...
} catch (IOException e) {
    // ...
}
一切正常。但我的服务器团队提到,有多个服务器请求正在从一个设备中命中。您正在创建多个打开的连接。尝试打开一个连接并从该连接发送所有请求。不要打开多个openConnection()

我核实了许多关于这方面的网站。但是没有运气。有没有人能让我知道,有没有可能在连接时发送所有请求

vhttpReadStream = httpsConnection.getInputStream();
                    vinpBytes = new byte[CVBUFFSIZE];
                    vbyteOs = new ByteArrayOutputStream(CVBUFFSIZE);
                    try {
                        while ((vintToRead = vhttpReadStream.read(vinpBytes)) != -1) {
                            vbyteOs.write(vinpBytes, 0, vintToRead);
                            cvtotalBytes += vintToRead;
                        }
                    } catch (Exception e) {
                        // #ifdef j2meuix.sop
                        Log.e(FILENAME, "output in Exception : " + vbyteOs.toString());
                        Log.e(FILENAME, "Exception in parsing resp : " + e);
                        // #endif
                    }

HttpURLConnection使用自动重用连接并将它们保留在连接池中。但是,这只有在正确使用它的情况下才有效。这意味着您必须始终读取返回的数据(通过
getInputStream()
resp.
getErrorStream()
)它提供的每个字节,直到到达流的末尾。无论发生什么错误,都要这样做!否则连接会被阻塞,无法重新使用。您能详细解释一下吗??因为我得到了getInputStream()并写入了ByteArrayOutputStream。但是我没有使用getErrorStream()。这会产生任何问题吗?您使用的
getOutputStream()
方向相反,这会将数据附加到请求中。我添加了
getOutputStream()
代码片段供您参考。