[英]Accessing URL on google app engine
I have written a servlet on app engine to call URL from nseindia.com, please refer below snippet of code 我已经在应用引擎上编写了一个servlet来从nseindia.com调用URL,请参考下面的代码片段
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
URL url = new URL("https://nseindia.com");
System.out.println("URL - " + url.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
//StringBuffer json = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
//json.append(line);
System.out.println("line-" + line);
}
reader.close();
}
This code works fine when i call from main method, but same thing is not working on app engine (throws below exception) 当我从main方法调用时,此代码可以正常工作,但同一件事在App Engine上不起作用(抛出以下异常)
<HTML><HEAD> <TITLE>Access Denied</TITLE> </HEAD><BODY> <H1>Access Denied</H1> You don't have permission to access "http://nseindia.com/" on this server.<P> Reference #18.64d70b17.1494045584.4ed8f9c1 </BODY> </HTML>
Please tell me do i need to set any attribute access this URL, i have to access this URL because my application will not work if i don't get the data from here. 请告诉我我是否需要设置访问此URL的任何属性,我必须访问此URL,因为如果我没有从这里获取数据,我的应用程序将无法工作。 I have followed the coding as mentioned in URL fetch 我已经按照网址提取中提到的编码进行操作
There is a similar question here with no answer 这里有一个类似的问题,没有答案
You are trying to make a https
request. 您正在尝试发出https
请求。 You have to specifically pass FetchOptions
as mentioned in their docs for issuing https requests [0] 您必须按照其文档中提到的FetchOptions
专门传递用于发出https请求[0]
Something along these lines should work - 遵循这些思路的东西应该起作用-
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
FetchOptions fetchOptions = FetchOptions.Builder.validateCertificate();
HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
request.setHeader(new HTTPHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"));
HTTPResponse response = fetcher.fetch(request);
[0] https://cloud.google.com/appengine/docs/standard/java/issue-requests#issuing_an_https_request [0] https://cloud.google.com/appengine/docs/standard/java/issue-requests#issuing_an_https_request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.