Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:从https url获取响应_Android_Post_Https - Fatal编程技术网

Android:从https url获取响应

Android:从https url获取响应,android,post,https,Android,Post,Https,您好 我正在开发一个Android应用程序,需要通过https打开一个url(带有POST参数)并获得响应 还有一个更复杂的问题,我有一个自签名证书。我还需要接受饼干 有人知道从哪里开始吗 非常感谢,Android附带了apache commons http库。 设置https post请求非常简单: HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz"); List<NameValuePair> n

您好

我正在开发一个Android应用程序,需要通过https打开一个url(带有POST参数)并获得响应

还有一个更复杂的问题,我有一个自签名证书。我还需要接受饼干

有人知道从哪里开始吗


非常感谢,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);
HttpPost=新的HttpPost(“https://yourdomain.com/yourskript.xyz");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“postValue1”、“我的价值”);
添加(新的BasicNameValuePair(“postValue2”,“第二个值”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpClient=new DefaultHttpClient();
HttpResponse response=client.execute(post);
HttpEntity=response.getEntity();
String responseText=EntityUtils.toString(实体);
Android使用commons http库的4.x版本,因为4.0以下的所有版本都已超出其生命周期

我无法确切地告诉您如何向HttpClient注册自签名证书,但mybe commons http文档有助于:


我设法让它与Cookie和未签名的https异步工作

我在这里使用了代码:

并在此处使用Brian Yarger的代码对未签名https进行了修改:

(将上述代码添加到HttpConnection.java中run()的开头)

为了让cookie正常工作,我必须修改一些代码(来自HttpConnection.java的文章片段):

案例帖子:
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的StringEntity(数据));
httpPost.addHeader(“Cookie”,Cookie.getCookie());
response=httpClient.execute(httpPost);
Header[]headers=response.getAllHeaders();

对于(int i=0;iHi,非常感谢您的回复。它工作得很好。但是无法让未签名的证书工作。另外,关于如何让您的代码异步工作,有什么想法吗?我不希望UI在等待响应时挂起。正如David所说,您必须启动一个新线程,并将http post操作放入run方法中。数据可以进行交换通过使用Androids处理程序系统在线程之间进行ged。您可能希望查看Android基础知识页面:
         case POST:
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(data));
            httpPost.addHeader("Cookie", Cookie.getCookie());
            response = httpClient.execute(httpPost);

            Header[] headers=response.getAllHeaders();
            for(int i=0;i<headers.length;i++){
                if(headers[i].getName().equalsIgnoreCase("Set-Cookie")){
                    //Log.i("i",headers[i].getName()+"---"+headers[i].getValue());
                    Cookie.setCookie(headers[i].getValue());
                    break;
                }
            }
            break;