如何使用android sdk从https服务器获得响应?

时间:2010-08-13 15:41:15

标签: android https

我将数据上传到https服务器时遇到ssl异常问题。它正确地将数据上传到服务器但是当我在上传后得到响应时抛出了一个ssl证书不受信任的例外。我正在使用SAX解析器来解析xml文件,我正在使用httppost方法()。

2 个答案:

答案 0 :(得分:1)

您必须添加新方案以接受安全站点连接

检查一下,在那里你会发现另一个有用的样本而不检查确认...

Https Connection Android

答案 1 :(得分:0)

Android自带了apache commons http库。设置https发布请求非常简单:

HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();

String responseText = EntityUtils.toString(entity);

Android使用commons http库的4.x版本,因为4.0以下的所有版本都在其生命周期之外。

我无法确切地知道如何向HttpClient注册自签名证书,但mybe the commons http文档有帮助:

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506