问候,
我正在开发一个 Android 应用程序,需要通过 https 打开一个 url(带有 POST 参数)并获取响应。
我有一个自签名证书,这让情况更加复杂。我还需要接受cookies。
有人知道从哪里开始吗?
提前谢谢了,
Android 附带了 apache commons http 库。
设置 https post 请求非常简单:
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 commons http 文档会有所帮助:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506 http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)