我正在使用类似以下代码的HttpURLConnection
:
String strURL = "https://example.herokuapp.com";
Bitmap bmImage = null;
HttpURLConnection connection = null;
InputStream in = null;
showMessage(context.getString(R.string.message_preparing));
try {
int timeoutMS = 15000;
URL url = new URL(strURL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setConnectTimeout(timeoutMS);
connection.setReadTimeout(timeoutMS);
connection.connect();
in = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
bmImage = BitmapFactory.decodeStream(in, null, options);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bmImage;
这段代码定义了一个URL并返回一个bmp图片,再由上述代码解码使用。对于一般用户而言,这段代码可正常工作,但对于某个用户,尽管该代码能够成功获取bmp图像,但在服务器(Heroku上的node.js服务器)中,他们的设备似乎还会发送一个CONNECT请求。虽然此请求会被自动拒绝并返回503响应,所以它本身不是问题,bmp图像仍将被发送到他们的设备,但我想知道为什么会发送这些CONNECT请求以及如何阻止它们。难道应该只有GET请求?
我已经尝试了这个解决方案,看起来与我的问题类似,但对我没有任何影响。
请注意,strURL是指向https服务器的,并且我正在使用HttpURLConnection(而不是Https)——不确定是否有任何重要性。
我也不能百分之百确定CONNECT请求来自上述调用,但它们肯定发生在传送bmp图像的GET请求前后。也许它可以通过操作系统在我的代码之外生成?不确定。
如果有帮助,下面是来自Heroku的一个示例日志消息,针对其中的一个CONNECT请求:
Oct 27 14:14:25 example heroku/router: at=error code=H13 desc="Connection closed without response" method=CONNECT path="example.herokuapp.com:443" host=example.herokuapp.com request_id=353e623x-dec4-42x5-bcfb-452add02ecef fwd="111.22.333.4" dyno=web.1 connect=0ms service=1ms status=503 bytes=0
编辑:这也可能与设备有关,因为该设备实际上在短时间内进行了两个独立的GET请求(完全不同且合法的请求),但是只有一个CONNECT请求显然存在(大约在一对GET请求的同时)。 因此,并非每个GET请求都有一个CONNECT请求。