我有一个执行 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"+
"¤cyCode=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/