gpt4 book ai didi

java - 访问谷歌应用引擎上的URL

转载 作者:行者123 更新时间:2023-12-02 13:15:13 24 4
gpt4 key购买 nike

我在应用程序引擎上编写了一个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();

}

当我从主方法调用时,此代码工作正常,但同样的事情在应用程序引擎上不起作用(抛出异常)

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;nseindia&#46;com&#47;" on this server.<P>
Reference&#32;&#35;18&#46;64d70b17&#46;1494045584&#46;4ed8f9c1
</BODY>
</HTML>

请告诉我是否需要设置任何属性来访问此 URL,我必须访问此 URL,因为如果我不从此处获取数据,我的应用程序将无法工作。我遵循URL fetch中提到的编码

有一个类似的问题here with no answer

最佳答案

您正在尝试发出 https 请求。您必须按照其文档中提到的方式专门传递 FetchOptions 来发出 https 请求 [0]

按照这些思路应该可以工作 -

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

关于java - 访问谷歌应用引擎上的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com