从Google App Engine中的HttpURLConnection读取数据时出现超时异常

时间:2016-11-04 12:37:20

标签: java google-app-engine servlets google-cloud-pubsub

我正在Google App Engine上运行一个应用程序,该应用程序调用我的Http URL Web应用程序(在Glassfish上运行)使用inputstream读取数据。这是 我的代码片段。

OutputStream os = response.getOutputStream();
URL url = new URL(Http URL);
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(0); 
conn.setReadTimeout(0);  
conn.setRequestMethod("GET");
System.out.println("response code=" + conn.getResponseCode());
byte b[] = new byte[2048];
InputStream is = url.openStream();
int len;
while ((len=is.read(b))!=-1){
    os.write(b, 0, len);
    System.out.println("len=" + len);
}

但是我得到了“在获取URL时超时:”例外。

我也试过了URLFetchService,但异常保持不变

URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
URL url = new URL(Http URL);
Future future = urlFetchService.fetchAsync(url);
HTTPResponse response1 = (HTTPResponse)future.get();
byte[] content = response1.getContent();
int responseCode = response1.getResponseCode();
List<HTTPHeader> headers = response1.getHeaders();
System.out.println("responseCode=" + responseCode);

请告知如何继续。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决了这个异常。我的Web应用程序的Http URL(在Glassfish上运行)是一个内部IP地址。在尝试从Google读取数据时,它应该是可公开访问的IP地址。