0
请告诉我如何通过Android上的https发送请求。 我试试这个。但是我得到IOExeption。Android和HTTPS请求
try {
HostnameVerifier hostnameVerifier = SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier)hostnameVerifier);
registry.register(new Scheme("https", socketFactory,443));
SingleClientConnManager mngr = new SingleClientConnManager(client.getParams(), registry);
trustEveryone();
DefaultHttpClient httpClient = new DefaultHttpClient(mngr,client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpPost httpPost = new HttpPost(URL);
StringEntity se = new StringEntity(obj);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = (HttpResponse)httpClient.execute(httpPost);
StatusLine status = response.getStatusLine();
if((status.getStatusCode())==200) {
HttpEntity entity = response.getEntity();
if(entity!=null) {
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
instream.close();
} else {
result=null;
}
}
} catch (ClientProtocolException e) {}
catch (IOException e) {}
显示,请您与logcat中精确的错误。没有这一点就很难猜测。 – Maxim 2012-01-31 14:53:48
我完成了这项任务。 – 2012-02-02 11:53:14
然后进行编辑并指出什么是错误的,这将有助于其他人。 – Maxim 2012-02-02 15:16:06