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