2013-05-02 70 views
0

我試圖發送請求到Https webservice,但它總是返回404 Not Found在此服務器上找不到請求URL),但它在瀏覽器中完美工作。它返回格式爲XML的響應。請幫助傢伙。如何在Android中向https webservice發送請求?

這裏是我的代碼

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(); 

    } 
+0

什麼是您的服務器日誌顯示? – CommonsWare 2013-05-02 18:38:07

+0

@CommonsWare請參閱[this](http://pastie.org/7754343) – moDev 2013-05-02 18:41:26

+0

**你的服務器日誌**顯示什麼? – CommonsWare 2013-05-02 18:51:09

回答

0

最後我已經解決了這個問題。我需要使用Simple HTTP Url Connection而不是上面的方法。

下面是代碼

 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(); 
      } 
0

試試這個代碼,它可以幫助你了...

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(); 
     }