0

When I am Running my RESTful client in Eclipse, getting the following error:

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
    at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at com.elkay.client.RestClient.main(RestClient.java:23)

When invoked in browser, the URL is not blocked. How do I use it through client?

TIA!

EDIT: (client code)

public class RestClient {

    public static void main(String[] args) {

          try {

            URL url = new URL("https://www.xyz.com");


            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            DataOutputStream printout = new DataOutputStream(conn.getOutputStream());  
            String authString=URLEncoder.encode("username=anushree&password=anushree","UTF-8");  
            printout.writeBytes (authString);  
            printout.flush ();   
            printout.close ();  

            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

          } catch (MalformedURLException e) {

            e.printStackTrace();

          } catch (IOException e) {

            e.printStackTrace();

          }

        }


}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Anushree Acharjee
  • 854
  • 5
  • 15
  • 30
  • edited. Please check. I am not using jersey or CFX. Just this simple client. – Anushree Acharjee Jan 22 '13 at 07:05
  • Why are you using HttpUrlConnection. You should be using HttpsUrlConnection. Also is the certificate added in your trusted store? – Satish Jan 22 '13 at 08:13
  • have tried with HttpsUrlConnection.Also, can you brief how do I create the trusted store i.e .jks file? because I am directly able to invoke the URL in browser, it is not blocked for using HTTPS. NOTE: https://www.xyz.com is not the exact URL, but likewise. – Anushree Acharjee Jan 22 '13 at 08:28
  • Have put the code in next answer. You can generate .crt from Mozilla browser of .cer from IE – Satish Jan 22 '13 at 08:35
  • Also what are you trying in these 2 lines?String authString=URLEncoder.encode("username=anushree&password=anushree","UTF-8"); printout.writeBytes (authString); – Satish Jan 22 '13 at 08:55
  • usually, for secured URLs, when run in Firefox, it pops up an window, where after accepting the same you may proceed and from tat pop up only we get the certificate. But When I am invoking this URL, as I said earlier, there is no pop up.there is a pop up, but tat is only for username/passwd. what should I do? – Anushree Acharjee Jan 22 '13 at 09:10
  • From certificate icon, go to certificate properties and I think in 2nd tab you can download the certificate. Almost similar with IE. In ie you click on certificate icon next to url window, view certificates, 2nd tab copy to file... – Satish Jan 22 '13 at 09:13

1 Answers1

0

Also try like this. //Set basic authentication in the header

 String userPassword = "password"; 
 String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); 
 conn.setRequestProperty("Authorization", "Basic " + encoding); 
 String parameters = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("user1password", "UTF-8"); 
 printout.writeBytes (parameters);  
Satish
  • 713
  • 1
  • 5
  • 18