我的一些用户在尝试连接时经常遇到此异常。
java.io.IOException: Unexpected response code for CONNECT: 400
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeTunnel(HttpsURLConnectionImpl.java:509)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:463)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
400 表示错误的请求,但相同的请求在几秒钟后成功。我仔细检查了请求,没有任何问题。我也检查了服务器日志,似乎请求甚至没有到达我们的服务器。
我正在使用一个简单HttpUrlConnection
的连接。
URL connectURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Encoding", "deflate, gzip");
connection.setRequestProperty("Content-Length", postParameters.length() + "");
conn.connect();
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(mRequestObj.getPostParameters());
writer.flush();
writer.close();
responseCode = conn.getResponseCode();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(conn.getInputStream());
}
catch(IOException io) {
if(conn.getErrorStream() != null)
bis = new BufferedInputStream(conn.getErrorStream());
else
throw io;
}
...
还有其他人遇到过这个问题吗?有什么可以帮助的吗?