왜 데이터에 대한 첫 번째 HTTP 요청이 너무 느립니까?android httpurlconnection이 데이터 연결이 느립니다.
다음 행에서 뭔가를 사용하고 있습니다. 3 개의 HTTP 요청을 연속적으로 (한 번에 하나씩) 한 다음 지정된 시간 동안 기다렸다가 3 회의 HTTP 요청을 반복합니다.
기본적으로 작동하는 keep-alive를 사용하고 있지만 첫 번째 HTTP 요청에서 3 HTTP 요청으로 시작할 때마다 2 초의 패널티가 있습니다. 첫 번째 HTTP 요청에만이 2 초의 벌칙이 있습니다. 200ms와 같은 연속적인 HTTP 요청을 기다리는 시간을 줄이면 모든 요청이 빠르며 처음 2 초의 페널티를 보지 않아도됩니다.
분명히 이것은 첫 번째 요청이 연결 유지 연결을 사용하지 않을 수도 있지만 실제로는 그렇지 않습니다. 서버에서 tcpdump를 실행하면 모든 요청이 닫히지 않고 새 연결을 다시 구축하지 않고 동일한 TCP 연결을 사용하고 있음을 분명히 알 수 있습니다. 응용 프로그램에서 대기하는 시간이 예를 들어 5 초인 동안 서버의 활성 상태 설정은 60 초로 설정됩니다. 또한 내가 WIFI로 전환 할 때 나는 볼 수 없다. 같은 시간에 모든 요청을 기다리는 것이 빠릅니다.
는 HTTP 연결 코드는 HttpURLConnection의를 사용하고 다음과 같습니다 :
m_res.error = null;
HttpURLConnection connection;
try {
connection = (HttpURLConnection)(new URL(m_url + "/" + m_call.command).openConnection());
connection.setDoOutput(true); // triggers POST.
connection.setDoInput(true);
connection.setRequestProperty("Accept-Charset", m_charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + m_charset);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Accept-Encoding", "gzip");
//connection.setChunkedStreamingMode(0);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(m_call.query_string.getBytes(m_charset));
InputStream response = null;
try {
response = connection.getInputStream();
if ("gzip".equals(connection.getContentEncoding())) {
response = new GZIPInputStream(response);
}
}
catch (IOException e) {
response = connection.getErrorStream();
}
if (response == null) {
m_res.error = "Connection Error";
}
else {
m_res.body = getAsString(response);
m_res.status = connection.getResponseCode();
}
}
catch (IOException e) {
e.printStackTrace();
m_res.error = "Connection Error";
}
finally {
if (output != null)
try {
output.close();
} catch (IOException logOrIgnore) {}
}
}
}
귀하의 질문은 ... –
왜 데이터 연결에 대한 첫 번째 HTTP 요청이 너무 느 립니까? – domcyrus