I'm trying to populate our database using some curl command like

curl --user username:password https://somewebsite.com
curl - d "name=New User Name&address=New User Address" https://somewebsite.com/api/add/new

The instructions that they gave to me uses curl but I want to do it in Java so I decided to use HttpClient here's my code so far but with no luck with authorization. I keep getting an "Access Denied" but I'm pretty sure my credentials are correct.

DefaultHttpClient client = new DefaultHttpClient();
try{
    client.getCredentialsProvider().setCredentials(new AuthScope(
                    "somewebsite.com", 443), new     UsernamePasswordCredentials(username,password));
    HttpPost postMethod = new HttpPost("https://somewebsite.com/api/add/new");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    populateVendor(nameValuePairs, data); //function to populate list
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(postMethod);
    InputStream stream = response.getEntity().getContent();
    System.out.println("JSON RESPONSE: "+json);
    //more functions to process JSON
}
finally{
   client.getConnectionManager.shutdown();
}

Which version of HttpClient are you using? 3.x? Also, I could have sworn the default port for HTTPS was 443...

I use 4.1. Thanks for the port advice I changed it but still I'm getting Access Denied

SOLVED: I disabled SSL Certificate using this code

TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}

Source: http://bit.ly/oTnIGH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.