Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3655

how to connect to REST web service from Java application

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

Viewing all articles
Browse latest Browse all 3655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>