英文:
JsonParser via proxy
问题
我已经有下面的代码在正常运行。但是在生产环境中,我需要通过代理服务器读取JSON。如何实现相同的功能?
我看到一些谷歌示例,使用https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/client/SimpleClientHttpRequestFactory.html来进行简单的REST请求通过代理,但不确定在这种情况下是否可以使用该方法来读取JSON。
JsonFactory jsonFactory = new JsonFactory();
URL productFeedFile = new URL("**URL**");
JsonParser jsonParser = jsonFactory.createParser(productFeedFile);
英文:
I have below code working fine. though on prod environment, i need to read the json via proxy server. How to achieve the same?
I can see some google examples with https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/client/SimpleClientHttpRequestFactory.html for simple REST requests via proxy, but not sure i can use that in this case for json reading
JsonFactory jasonFactory = new JsonFactory();
URL productFeedFile = new URL("**URL");
JsonParser jsonParser = jasonFactory.createParser(productFeedFile);
答案1
得分: 1
//Proxy 实例,代理 IP = 10.0.0.1,端口为 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
URLConnection conn = new URL(urlString).openConnection(proxy);
InputStream productFeedFileAsStream = conn.getInputStream();
JsonParser jsonParser = jasonFactory.createParser(productFeedFileAsStream);
英文:
Well I think that your JsonFactory converts the URL into an InputStream under the hood. If you do it on your own you could try:
//Proxy instance, proxy ip = 10.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
URLConnection conn = new URL(urlString).openConnection(proxy);
InputStream productFeedFileAsStream = conn.getInputStream();
JsonParser jsonParser = jasonFactory.createParser(productFeedFileAsStream);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论