Android:如何使用证书进行 HttpPost

标签 android paypal http-post

我有一个执行 HttpPost 的应用程序。

现在我需要向帖子添加一个证书,以便接收 HttpPost 的服务器接受。

请问我该怎么做?

非常感谢任何建议!!!

HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("https://svcs.sandbox.paypal.com/AdaptivePayments/Preapproval");
    try {

        httppost.addHeader("X-PAYPAL-SECURITY-USERID", "maurizio.pietrantuono_api1.db.com");
        httppost.addHeader("X-PAYPAL-SECURITY-PASSWORD", "1395657583");
        httppost.addHeader("X-PAYPAL-SECURITY-SIGNATURE", "A0GgTivJ6ivBB8QDTl.cZfiYK5d9AZwsFixwIUdUhJc4JXTriwpfU2zw");
        httppost.addHeader("X-PAYPAL-REQUEST-DATA-FORMAT", "NV");
        httppost.addHeader("X-PAYPAL-RESPONSE-DATA-FORMAT", "NV");
        httppost.addHeader("X-PAYPAL-APPLICATION-ID", "APP-80W284485P519543T");

        StringEntity se=new StringEntity("cancelUrl=http://your_cancel_url"+
"&currencyCode=USD"+
"&endingDate=2015-03-29T08%3A00%3A00.000Z"+
"&maxAmountPerPayment=200.00"+
"&maxNumberOfPayments=30"+
"&maxTotalAmountOfAllPayments=1500.00"+
"&pinType=NOT_REQUIRED"+
"&requestEnvelope.errorLanguage=en_US"+
"&returnUrl=http://www.google.com"+
"&startingDate=2014-04-29T07%3A00%3A00.000Z"+
"&senderEmail=mauriziop-facilitator@hotmail.it");
        httppost.setEntity(se);

        HttpResponse response = httpclient.execute(httppost);

最佳答案

这是由于 android 应用程序不接受服务器提供的安全证书引起的。有时您可能已经看到浏览器要求获得继续的许可,并说“该站点的安全证书不受信任!”。同样的事情在android中产生异常。所以你必须告诉应用程序接受服务器提供的任何安全证书。这是怎么做的。 http://madurangasblogs.blogspot.com/2013/08/avoiding-javaxnetsslsslpeerunverifiedex.html

但我必须提醒您,这对于生产应用程序来说不是一个好的做法。这将违背拥有安全证书的目的。

此外尝试这样的事情(你需要让你的套接字工厂使用这个默认的信任管理器):

X509TrustManager manager = null;
FileInputStream fs = null;

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

try
{
    fs = new FileInputStream(System.getProperty("javax.net.ssl.trustStore")); 
    keyStore.load(fs, null);
}
finally
{
    if (fs != null) { fs.close(); }
}

trustManagerFactory.init(keyStore);
TrustManager[] managers = trustManagerFactory.getTrustManagers();

for (TrustManager tm : managers)
{
    if (tm instanceof X509TrustManager) 
    {
        manager = (X509TrustManager) tm;
        break;
    }
}

资料来源: http://android.bigresource.com/Android-Issues-with-httppost-Authentication-challenge-is-empty-Gg4BTT7Dr.html Get the authentication from the moodle for a android application

关于Android:如何使用证书进行 HttpPost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22744517/

相关文章:

paypal - 通过 REST API 获取货币转换后的金额

c# - 如何使用C#提交http表单

java - 如何将 java 对象传递给 HTTP Post 参数

php - Paypal API 更新订单运输公司、订单状态和跟踪号码

c# - 向 IIS 上传超过 1MB 的数据时连接速度慢

android - 使用 app-debug.apk 在我的设备上安装

android - 手机调试连接USB时如何导入外部sqlite数据库进行编程?

android - 导入 android-support-v7-appcompat 错误时出错 : Error: No resource found

android - MediaPlayer:在使用Phaser 2.4.6的Cordova 6.1.1中出现错误(1,-2147483648)

php - Magento 1.5 Paypal Express 评论页面运输 - 没有可用的报价