0

我必须测试 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。我出于我的目的修改了这个。

  1. 使用证书,它会在该行引发错误
  2. 如果没有证书(已注释掉),连接将超时并在 getResponseCode() 处引发异常。

我不确定:

  1. 提交证书的正确方法是什么
  2. 如果我正确发送凭据
  3. 如果我的代码不完整,因此应用程序无法获取响应代码
  4. 我应该使用 Eclipse EE(带有 Web 工具平台)并创建项目 > Web 应用程序,而不是 Eclipse Juno(没有 WTP)

先感谢您。

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

    }
}
4

3 回答 3

0

你不需要自己动手。

于 2013-02-07T00:03:04.027 回答
0

在 REST-Clients 的所有框架中……你尝试过 OpenFeign 吗?它是来自 NetFlix 堆栈的组件。易于使用并适合 NetFlix 的所有其他组件。

试一试:https ://github.com/OpenFeign/feign

于 2019-09-04T08:11:58.737 回答
0

您正在使用 DER 文件作为Java Crypto通常不支持的密钥库。使用 keytool 创建 JKS 或其他受支持的密钥库,然后引用它。

于 2016-03-25T01:15:43.130 回答