如何HTTPS POST Android中 [英] How to HTTPS post in Android

查看:165
本文介绍了如何HTTPS POST Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过下面的链接,但似乎没有任何具体的。 安全HTTP张贴在Android的 这其中不工作了,我已经测试过它,并有从其他人的评论说,这是行不通的。

I have looked at the following links, but nothing seems concrete. Secure HTTP Post in Android This one does not work anymore, I have tested it and there are comments from other people saying it does not work.

我还检查了这一点:<一href="http://stackoverflow.com/questions/5629122/defaulthttpclient-certificates-https-and-posting-problem?rq=1">DefaultHttpClient,证书,HTTPS和张贴问题!这似乎是它可以工作,但博客只是让你挂。更多一步一步的指示将是有益的。我设法让我的证书由我一直没能贯彻他的第二个步骤。

I also checked this out: DefaultHttpClient, Certificates, Https and posting problem! This seems it could work but the blogger just leaves you hanging. More step by step instructions would be helpful. I managed to get my certificate by I have not been able to follow through his second step.

<一个href="http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html">http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html这其中似乎不错,但再一次,我失去的作者在最后一步:回到我们最初的REST客户端code他也是所有的地方,我不知道该库他正在使用。他没有解释他的code和与

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html This one seem good, but again, I loose the author at the last step: "Back to our original rest client code." He too is all over the place, I have no clue which libraries he is using. He is not explaining his code and with the

RestTemplate restTemplate = new RestTemplate();

它的另一个悬念。因为那类尚未提供。所以,如果有人可以解释如何做了详细的HTTPS POST请求,这将是巨大的。我也需要接受自签名证书。

it's another cliffhanger. Because that class has not been provided. So, if someone could explain how to do HTTPS post request in detail that would be great. I do need to accept the self signed certificate.

推荐答案

我希望这会有所帮助。这是在$ C $词使用,完全正常工作。

I hope it would help. This is the code i used and worked perfectly fine.

private HttpClient createHttpClient()
{
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

然后创建一个HttpClient的是这样的: -

Then create an HttpClient like this: -

HttpClient httpClient = createHttpClient();

和使用它的HttpPost。

and use it with HttpPost.

干杯!!

修改

和我没用过RestTemplate在我的code。我做了一个简单的post请求。如果您需要更多的帮助,只是让我知道。好像我最近做了类似于你在找什么东西。

And i did not used RestTemplate in my code. I made a simple post request. If you need more help just let me know. It seems like i recently have done something similar to what you are looking for.

这篇关于如何HTTPS POST Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆