Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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
Php 如何在Android中向https Web服务发送请求?_Php_Android_Web Services_Https - Fatal编程技术网

Php 如何在Android中向https Web服务发送请求?

Php 如何在Android中向https Web服务发送请求?,php,android,web-services,https,Php,Android,Web Services,Https,我正在尝试向Https Web服务发送请求,但它总是返回404未找到(此服务器上未找到请求url),但它在浏览器中工作正常。它以XML格式返回响应。请帮帮我 这是我的密码 try { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", SSLSocketFactory .getSo

我正在尝试向Https Web服务发送请求,但它总是返回
404未找到
此服务器上未找到请求url),但它在浏览器中工作正常。它以
XML
格式返回响应。请帮帮我

这是我的密码

    try {

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", SSLSocketFactory
                .getSocketFactory(), 443));

        HttpParams param = new BasicHttpParams();

        SingleClientConnManager mgr = new SingleClientConnManager(param,
                schemeRegistry);

        HttpClient client = new DefaultHttpClient(mgr, param);
        HttpPost post = new HttpPost(Global.CARD_API_URL);

        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("user", "xxx"));
        parameters.add(new BasicNameValuePair("pass", "xxxxxxxx"));
        parameters.add(new BasicNameValuePair("cardNumber",
                "xxxxxxxxxxxxxxxx"));
        parameters.add(new BasicNameValuePair("expiryDate", "0515"));
        parameters.add(new BasicNameValuePair("cvc", "123"));


        post.setEntity(new UrlEncodedFormEntity(parameters));

        HttpResponse response = client.execute(post);

        String res = EntityUtils.toString(response.getEntity());

        Log.d("Card Details", res);

    } catch (Exception e) {

        e.printStackTrace();

    }
试试看{
SchemeRegistry SchemeRegistry=新SchemeRegistry();
注册(新方案(“https”),SSLSocketFactory
.getSocketFactory(),443));
HttpParams param=新的BasicHttpParams();
SingleClientConnManager mgr=新的SingleClientConnManager(参数,
计划地理学);
HttpClient客户端=新的默认HttpClient(管理器,参数);
HttpPost=新的HttpPost(Global.CARD\u API\u URL);
列表参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户”,“xxx”));
添加(新的BasicNameValuePair(“通过”、“xxxxxxxx”));
添加(新的BasicNameValuePair(“卡号”),
“XXXXXXXXXXXXXX”);
添加(新的BasicNameValuePair(“expiryDate”、“0515”);
添加(新的BasicNameValuePair(“cvc”,“123”));
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=client.execute(post);
String res=EntityUtils.toString(response.getEntity());
Log.d(“卡详细信息”,res);
}捕获(例外e){
e、 printStackTrace();
}

试试这段代码,它可能会帮你解决问题

SSLContext ctx;
        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[] {
                      new X509TrustManager() {
                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
                      }
                    }, null);
            HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

我终于解决了这个问题。我需要使用简单的HTTP Url连接,而不是上述方法

这是密码

      try {
            url = new URL(api_url);

            URLConnection connection = url.openConnection();
            HttpURLConnection http_connection = (HttpURLConnection) connection;
            http_connection.setConnectTimeout(10000);

            // Set the appropriate HTTP parameters.
            http_connection.setRequestMethod("POST");
            http_connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded; charset=utf-8");

            http_connection.setDoOutput(true);
            http_connection
                    .setFixedLengthStreamingMode(post_data.getBytes().length);

            DataOutputStream dos = new DataOutputStream(
                    http_connection.getOutputStream());

            dos.writeBytes(post_data);
            dos.flush();
            dos.close();

            // Read the response.
            InputStreamReader isr = new InputStreamReader(
                    http_connection.getInputStream());
            BufferedReader in = new BufferedReader(isr);

            sb = new StringBuffer();

            // Write the message response to a String.
            while ((response = in.readLine()) != null) {

                sb.append(response);
            }

               Log.d("Card Details", sb.toString());
            }catch(Exception e){

                     e.printStackTrace();
            }

您的服务器日志显示什么?@commonware查看您的服务器日志显示什么?@commonware我不处理服务器部分。