繁体   English   中英

jersey:以json文件的形式接收来自Web服务的响应

[英]jersey : receive response from web-service as json file

nni在使用jersey客户端调用Web服务时遇到问题。 我成功地通过以下方式进行了测试:“ http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2248907%22&format=json

使用此代码:

Client client = Client.create();
WebResource webResource = client.resource("http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2248907%22&format=json");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String json = response.getEntity(String.class);
System.out.println(json);

但是当我调用亚马逊网络服务时我无法做到这一点: http ://ws.amazon.com/widgets/q?Operation = GetResults&Keywords = cool&SearchIndex = All&multipageStart =0& InstanceId =0& multipageCount=10&TemplateId = 8002&ServiceVersion = 20070822&MarketPlace= US

是因为我得到一个json文件作为响应吗?

有什么帮助吗?

在尝试使用各种形式的HTTP请求的Amazon Web服务之后。 我最终发现问题是由于HTTP标头中发送了User-Agent值。

由于某些原因,Amazon Rest Service无法处理句号字符的存在。 在User-Agent下的HTTP标头中。

使用发送HTTP请求时。 如下

GET http://ws.amazon.com/widgets/q?Operation=GetResults&Keywords=cool&SearchIndex=All&multipageStart=0&InstanceId=0&multipageCount=10&TemplateId=8002&ServiceVersion=20070822&MarketPlace=US HTTP/1.1
User-Agent: Java.
Host: ws.amazon.com
Connection: keep-alive

Amazon WS发送不带正文内容的HTTP响应

HTTP/1.1 200 OK
Date: Fri, 27 Sep 2013 19:29:54 GMT
Server: Server
Content-Length: 0
Vary: Accept-Encoding,User-Agent
Cneonction: close
Content-Type: text/plain

如果。 从Content-Type中删除,响应主体确实包含详细的Json Content。 这很可能看起来像Amazon Rest Service实施中的问题。

您可以按如下所示更改代码以查看Json内容并解决该问题

 ClientResponse response = webResource.header("User-Agent", "SomeAgentNameWithoutPeriodChar").get(ClientResponse.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM