在Android中执行POST请求

时间:2012-03-30 19:39:06

标签: android http

我基本上试图让我的应用程序执行以下命令

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

但我不明白到目前为止我找到的解决方案。如果我使用HttpPost发布nameValuePairs应该是什么?

当httpclient.execute(httppost)时,我也会收到信息ssl-certificate-not-trusted-error的IOException;命令已完成。

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

2 个答案:

答案 0 :(得分:1)

如果您想忽略SSL证书错误,因为您确实信任该站点,您需要提供自己的TrustManager信任所有证书。

请参阅:HTTPS and self-signed certificate issue

答案 1 :(得分:0)

也许尝试这种方式进行身份验证:

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
}
相关问题