如何从 Java 应用程序连接到 REST Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14741133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to connect to REST web service from Java application
提问by dhananjay
I have to test the EPA's Data Exchange Web Services. Since it is difficult to create 100 accounts, buildings, energy usage distributions, etc. I want to automate the process. I searched for code examples to do a simple GET. The best one I found was at http://pic.dhe.ibm.com/infocenter/tivihelp/v10r1/index.jsp?topic=%2Fcom.ibm.taddm.doc_7.2%2FSDKDevGuide%2Ft_cmdbsdk_restapi_java.html. I modified this for my purposes.
我必须测试 EPA 的数据交换 Web 服务。由于很难创建 100 个帐户、建筑物、能源使用分布等。我想使该过程自动化。我搜索了代码示例来做一个简单的 GET。我发现的最好的是http://pic.dhe.ibm.com/infocenter/tivihelp/v10r1/index.jsp?topic=%2Fcom.ibm.taddm.doc_7.2%2FSDKDevGuide%2Ft_cmdbsdk_restapi_java.html。我出于我的目的修改了这个。
- With the certificate, it is throwing an error at that line
- Without the certificate (commented out), the connection is timing out and throwing the exception at getResponseCode().
- 使用证书,它在该行抛出错误
- 如果没有证书(注释掉),连接就会超时并在 getResponseCode() 处抛出异常。
I'm not sure:
我不确定:
- What is the correct way of submitting a certificate
- If I am sending the credentials correctly
- If my code is incomplete, and therefore, the application is unable to get the response code
- I should be using Eclipse EE (with Web Tools Platform) and create Project > Web Application, instead of Eclipse Juno (without WTP)
- 什么是提交证书的正确方式
- 如果我正确发送凭据
- 如果我的代码不完整,因此应用程序无法获取响应代码
- 我应该使用 Eclipse EE(带有 Web 工具平台)并创建项目 > Web 应用程序,而不是 Eclipse Juno(没有 WTP)
Thank you in advance.
先感谢您。
package Package1;
import java.io.*;
import java.util.*;
import java.lang.StringBuffer;
import java.net.*;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
public class Class1 {
public static void main (String args[]){
try{
// set this property to the location of the cert file
System.setProperty("javax.net.ssl.trustStore","C:/Documents and Settings/bhattdr/Desktop/-.energystar.gov.der");
String username = "yy777PPP";
String password = "yy777PPP";
String userpass = "";
URL url = new URL("https://portfoliomanager.energystar.gov/wstest/account");
// URLConnection uc = url.openConnection();
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
userpass = username + ":" + password;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
System.out.println("sending request...");
uc.setRequestMethod("GET");
uc.setAllowUserInteraction(false);
uc.setDoOutput(true);
uc.setRequestProperty( "Content-type", "text/xml" );
uc.setRequestProperty( "Accept", "text/xml" );
uc.setRequestProperty ("Authorization", basicAuth);
System.out.println(uc.getRequestProperties());
// uc.setRequestProperty( "authorization", "Basic " + encode("administrator:collation"));
// Map headerFields = uc.getHeaderFields();
// System.out.println("header fields are: " + headerFields);
int rspCode = uc.getResponseCode();
if (rspCode == 200) {
InputStream is = uc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String nextLine = br.readLine();
while (nextLine != null) {
System.out.println(nextLine);
nextLine = br.readLine();
}
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
回答by EdH
You don't need to roll your own.
你不需要自己滚动。
- If you want to write something, you can use Jersey, which has existing classes to act as Rest Clients (Rest clients for Java?)
- There are plenty of apps which exercise rest apis which you can use if you don't want to write something. Google turns up plenty (like http://code.google.com/p/rest-client/)
- 如果你想写一些东西,你可以使用 Jersey,它有现有的类来充当 Rest Clients(Java 的 Rest 客户端?)
- 有很多应用程序可以使用 rest api,如果您不想写东西,可以使用它们。谷歌出现了很多(比如http://code.google.com/p/rest-client/)
回答by Archimedes Trajano
You're using a DER file as your key store which is not supported by Java Cryptonormally. Use the keytool to create a JKS or some other supported keystore and then refer to it.
您正在使用 DER 文件作为您的密钥库,Java Crypto通常不支持该文件。使用 keytool 创建 JKS 或其他一些受支持的密钥库,然后引用它。
回答by Carsten Spr?ner
AMong all the frameworks for REST-Clients... did you try OpenFeign? It's a components from the NetFlix stack. Easy to use and fits into all the other components of NetFlix.
在 REST 客户端的所有框架中……您尝试过 OpenFeign 吗?它是来自 NetFlix 堆栈的组件。易于使用并适合 NetFlix 的所有其他组件。
Give it a try: https://github.com/OpenFeign/feign