JsonParser 通过代理

huangapple go评论114阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2020年7月30日 19:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定