从我的Android应用程序中,我想要向服务器发送数据并获取响应,然后处理它,再发送并获取另一个请求。由于这是持续通信,直到没有更多的响应需要处理,我选择使用
而我重复使用套接字的尝试是成功的,但我面临的问题是:
http.keepAlive = true
的HttpURLConnection
。而我重复使用套接字的尝试是成功的,但我面临的问题是:
- 我正在尝试从客户端(Android应用)启动关闭,因为如果终止从服务器开始,那么服务器将进入
TIME_WAIT
状态。我不希望我的服务器进入该状态,因此我更喜欢客户端启动终止。但不幸的是,我发现没有适合使用HttpURLConnection
的方法来执行此操作。 - 经过数小时的搜索,我放弃了上述尝试,并根据
keepalivetimeout
从服务器启动关闭,但当服务器发送FIN
时,客户端仅回复ACK
,因此连接在服务器上保持在FIN_WAIT_2
状态和代理上的CLOSE_WAIT
状态。
源代码:
private HttpStatus communicateWithServer(String httpUrl, String dataToSend, boolean keepAlive) {
HttpStatus status = new HttpStatus(HTTP_STATUS_FAILURE);
try {
initializeConnection(httpUrl,keepAlive);
postDataToConnection(connection, dataToSend);
status = readDataFromConnection(connection);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
readErrorStreamAndPrint(connection);
}
connection.disconnect();
return status;
}
/**
* API to close connection, calling this will not force the connection to shutdown
* this will work based on the Connection header set.
* @param connection
*/
public void closeConnection(){
if(connection != null){
connection.disconnect();
}
}
/**
* Used to initialize the HttpURLConnection for the given url
* All properties required for connection are preset here
* Connection Time Out : 20 Seconds
* Connection Type : keep alive
* Content Type : application/json;charset=UTF-8
* And also All certificates will be evaluated as Valid.[ TODO will be removed soon]
* @param httpUrl
* @return
* @throws MalformedURLException
* @throws IOException
*/
private void initializeConnection(String httpUrl, boolean keepAlive) throws MalformedURLException, IOException{
URL url = new URL(httpUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST"); //NO I18N
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); //NO I18N
connection.setRequestProperty("Connection", "Keep-Alive"); //NO I18N
}
/**
* API to post data to given connection
* call to this API will close the @OutputStream
* @param connection
* @param data
* @throws IOException
*/
private void postDataToConnection(URLConnection connection , String data) throws IOException{
OutputStream outStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream));
writer.write(data);
writer.flush();
writer.close();
outStream.close();
}
/**
* API to read error stream and log
* @param connection
*/
private void readErrorStreamAndPrint(URLConnection connection){
try{
InputStream inStream = ((HttpURLConnection) connection).getErrorStream();
String responseData = "";
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(inStream));
while ((line=br.readLine()) != null) {
responseData+=line;
}
} catch (IOException ioe) {
}
}
/**
* API to read data from given connection and return
* call to this API will close the @InputStream
* @param connection
* @return
* @throws IOException
*/
private HttpStatus readDataFromConnection(URLConnection connection) throws IOException{
HttpStatus status = new HttpStatus(HTTP_STATUS_FAILURE);
int responseCode=((HttpURLConnection) connection).getResponseCode();
InputStream inStream = connection.getInputStream();
String responseData = "";
if (responseCode == HttpURLConnection.HTTP_OK) {
responseData = readStreamAsString(inStream);
status.setStatus(HTTP_STATUS_SUCCESS);
status.setUrlDataBuffer(responseData);
}
else {
status.setStatus(HTTP_STATUS_FAILURE);
}
inStream.close();
return status;
}
/**
* Read the InputStream to String until EOF
* Call to this API will not close @InputStream
* @param inStream
* @return
* @throws IOException
*/
private String readStreamAsString(InputStream inStream) throws IOException{
StringBuilder responseData = new StringBuilder();
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(inStream));
while ((line=br.readLine()) != null) {
responseData.append(line);
}
return responseData.toString();
}
有人可以帮忙吗?
HttpURLConnection
的适当方法是HttpURLConnection.disconnect()
。但这只是一个提示。 - user207421