0

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.

  1. With the certificate, it is throwing an error at that line
  2. Without the certificate (commented out), the connection is timing out and throwing the exception at getResponseCode().

I'm not sure:

  1. What is the correct way of submitting a certificate
  2. If I am sending the credentials correctly
  3. If my code is incomplete, and therefore, the application is unable to get the response code
  4. I should be using Eclipse EE (with Web Tools Platform) and create Project > Web Application, instead of Eclipse Juno (without 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();
        }

    }
}
Usha
  • 1,458
  • 11
  • 13
dhananjay
  • 41
  • 2
  • 2
  • 4
  • I know it´s late... but have you tried to remove the **uc.setRequestProperty( "Accept", "text/xml" );** line?? – VaroX Dec 20 '13 at 10:14

3 Answers3

0

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/)
Community
  • 1
  • 1
EdH
  • 4,918
  • 4
  • 24
  • 34
  • Yes I know that I can use these clients. However, I should be able to execute some variation of this code. The problem is that it is failing at any line with, for example, connect(), getResponseCode, getContent, etc. Basically it's not connecting and timing out. But it works using Chrome or Firefox client. If it works, I can run a script to do multiple POSTs required to create an account with valid properties, meters, etc. to generate metrics (which is what I'm testing). Why is it not connecting? – dhananjay Feb 13 '13 at 21:56
0

You're using a DER file as your key store which is not supported by Java Crypto normally. Use the keytool to create a JKS or some other supported keystore and then refer to it.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
0

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.

Give it a try: https://github.com/OpenFeign/feign