我正在使用此 solution 发出服务器请求。
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
}