【发布时间】:2015-08-18 20:20:19
【问题描述】:
从我的 Android 应用程序中,我想将数据发布到服务器并获取响应,处理它然后发回并获取另一个请求。由于是持续沟通,直到不再响应流程,我更喜欢 HttpURLConnection 和 http.keepAlive = true。
而且我重用socket的尝试是成功的,但是我面临的问题是:
- 我正在尝试从客户端(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' isHttpURLConnection.disconnect() 的合适方法。”但这只是一个提示。 -
@EJP 我在交易结束时使用了 disconnect() 但我没有发现任何区别,请您详细说明一下。
标签: java android tcp httpurlconnection keep-alive