Corith
Corith

Reputation: 31

Executing a POST-request in Android

I'm basically trying to get my app to do the following command

curl -i -X POST -d "user=USER&pass=PASS" https://websitehere.com

But I don't understand the solutions I've found so far. If I use a HttpPost to post nameValuePairs what should these be?

I also get an IOException with the information ssl-certificate-not-trusted-error when the httpclient.execute(httppost); command is done.

public void postData(String userString, String passString) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://websitegoeshere.com");
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("user", userString));
            nameValuePairs.add(new BasicNameValuePair("pass", passString));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));


            response = httpclient.execute(httppost);



        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {               
            e.printStackTrace();
        }
    }

Upvotes: 0

Views: 219

Answers (2)

dule
dule

Reputation: 18168

Perhaps try this means to authenticate:

HttpPost post = new HttpPost("https://websitegoeshere.com");
try {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    post.addHeader(new BasicScheme().authenticate(creds, post));
} catch (AuthenticationException e) {
    // Handle Exception
}

Upvotes: 0

dule
dule

Reputation: 18168

If you want to ignore the SSL cert error, because you do trust the site, you need to provide your own TrustManager which trusts all certificates.

Refer to: HTTPS and self-signed certificate issue

Upvotes: 1

Related Questions