【发布时间】:2017-09-23 05:10:32
【问题描述】:
我在 AWS Lambda 上使用 Java 来获取站点的 URL 源代码。我有以下代码:
URL yahoo = new URL(url);
URLConnection yc = yahoo.openConnection();
yc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader in = new BufferedReader(newInputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)a.append(inputLine);
in.close();
System.out.println(a.toString());
对于某些网站,代码运行得非常好。每次在我的本地机器上运行良好。但是,在 AWS Lambda 上运行时,它会卡在以下部分:
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
然后我得到:任务在 20.00 秒后超时。
在 Lambda 日志中,我收到以下错误:
有效负载:java.nio.HeapByteBuffer[pos=0 lim=115 cap=115]
我的猜测是,它与编码有关吗?为什么有些网站处理得非常好,而有些网站却卡在那行代码上?
非常感谢所有答案。
【问题讨论】:
-
其中一些问题可能与运行 AWS 的 VPC 有关。检查您的 VPC 配置,看看是否有任何设置需要微调。
标签: java amazon-web-services aws-lambda bufferedreader