使用 Java 客户端的 OData 服务的 400 响应
400 Respone from OData service using Java client
Chrome中的www.odata.org website demonstrates how to consume OData services 'in 6 steps'. I'm stuck on Step 3 of the examples located directly on the homepage. I am purposely using only the standard Java 7 libraries. The below code produces a 400 response. I have verified that the URL is correct by attempting the same query using the Advanced Rest Client。
任何人都可以告诉我如何仅使用标准 Java 7 个库成功完成第 3 步吗?
URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget gt 3000)");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("accept", "application/json");
c.setRequestProperty("Content-Type", "application/json");
c.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String input;
StringBuilder sb = new StringBuilder();
while ((input = in.readLine()) != null) {
sb.append(input);
}
in.close();
System.out.println(sb);
试试把c.getInputStream()改成c.getOutputStream()∀:
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
-->
BufferedReader in = new BufferedReader(new InputStreamReader(c.getOutputStream()));
顺便说一句,如果您想创建一个 java 客户端来使用 OData 服务,我会推荐 Apache Olingo java 客户端。 @ https://git-wip-us.apache.org/repos/asf?p=olingo-odata4.git;a=blob;f=fit/src/test/java/org/apache/olingo/fit/proxy/v4/EntitySetTestITCase.java;h=ccf956d4fc818ee0545a28bf8f6d8b3461b7da5c;hb=HEAD ,这是一个测试用例,显示查询 OData 服务是多么容易:
for (Customer customer : container.getCustomers())
{
assertNotNull(customer);
count++;
}
好吧,你需要像这样在 URL 上编码空格:URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget%20gt%203000)");
这是工作代码:
URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget gt 3000)");
URL urlToEncode = new URL(url);
URI uri = new URI(urlToEncode.getProtocol(), urlToEncode.getUserInfo(), urlToEncode.getHost(), urlToEncode.getPort(), urlToEncode.getPath(), urlToEncode.getQuery(), urlToEncode.getRef());
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection();
c.setRequestProperty("accept", "application/json");
c.setRequestMethod("GET");
BufferedReader in = new BufferedReader(newInputStreamReader(c.getInputStream()));
查看更多here
Chrome中的www.odata.org website demonstrates how to consume OData services 'in 6 steps'. I'm stuck on Step 3 of the examples located directly on the homepage. I am purposely using only the standard Java 7 libraries. The below code produces a 400 response. I have verified that the URL is correct by attempting the same query using the Advanced Rest Client。
任何人都可以告诉我如何仅使用标准 Java 7 个库成功完成第 3 步吗?
URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget gt 3000)");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("accept", "application/json");
c.setRequestProperty("Content-Type", "application/json");
c.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String input;
StringBuilder sb = new StringBuilder();
while ((input = in.readLine()) != null) {
sb.append(input);
}
in.close();
System.out.println(sb);
试试把c.getInputStream()改成c.getOutputStream()∀:
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
-->
BufferedReader in = new BufferedReader(new InputStreamReader(c.getOutputStream()));
顺便说一句,如果您想创建一个 java 客户端来使用 OData 服务,我会推荐 Apache Olingo java 客户端。 @ https://git-wip-us.apache.org/repos/asf?p=olingo-odata4.git;a=blob;f=fit/src/test/java/org/apache/olingo/fit/proxy/v4/EntitySetTestITCase.java;h=ccf956d4fc818ee0545a28bf8f6d8b3461b7da5c;hb=HEAD ,这是一个测试用例,显示查询 OData 服务是多么容易:
for (Customer customer : container.getCustomers())
{
assertNotNull(customer);
count++;
}
好吧,你需要像这样在 URL 上编码空格:URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget%20gt%203000)");
这是工作代码:
URL url = new URL("http://services.odata.org/V4/TripPinServiceRW/People?$top=2&$filter=Trips/any(d:d/Budget gt 3000)");
URL urlToEncode = new URL(url);
URI uri = new URI(urlToEncode.getProtocol(), urlToEncode.getUserInfo(), urlToEncode.getHost(), urlToEncode.getPort(), urlToEncode.getPath(), urlToEncode.getQuery(), urlToEncode.getRef());
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection();
c.setRequestProperty("accept", "application/json");
c.setRequestMethod("GET");
BufferedReader in = new BufferedReader(newInputStreamReader(c.getInputStream()));
查看更多here