android HTTPS POST Params

时间:2011-05-27 11:01:14

标签: android post webview

目前我正在关注流程

i)
    |-----------------
    |( context app)  |
    | purchase coins |
    | select how much|---->ii
    |                |
    |                |
    |                |
    |________________|

 ii) 

|-----------------
|(cotext website)| 
|copmany website |
|                |
|again give all  |
|prev params     |--->iii
|sends these     |
|params as POST  |
|req             |
|________________|     

iii)
|-----------------
| ctx website    |
| secure https   | 
| purchase coins |
| done           |
|                |
|                |
|                |
|________________|

在第一步中,用户选择他想要购买的金额。 然后用户被带到公司网站,在那里他再次选择这些选项,然后将这些参数作为POST请求发布到安全网站,然后用户必须填写有关卡等的信息。

我想要的是跳到安全网站省略公司网站,但如何将这些值发布到安全网站,然后在浏览器中显示响应???

更新 到目前为止,我有以下代码,它提供 HTTP 200 OK ,但内容长度为-1

public static void postData(String url, String coinsValue) throws ClientProtocolException, IOException {
        byte[] result = null;

        SchemeRegistry registry = new SchemeRegistry();




        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URLUtil.guessUrl(url));

        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);     

        httppost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " + "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
        httppost.setHeader("Accept", "text/html,application/xml," + "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("gae", CoreConstants.USERINFO.getName()));
        nameValuePairs.add(new BasicNameValuePair("param1", "1"));
        nameValuePairs.add(new BasicNameValuePair("quantity", "1"));
        nameValuePairs.add(new BasicNameValuePair("g_noteParam", coinsValue));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        int StatusCode= response.getStatusLine().getStatusCode();
        String stautsLine = response.getStatusLine().toString();

        HttpEntity r_entity = response.getEntity();

        if (r_entity != null) {
            int contentLength = (int)r_entity.getContentLength();
            result = new byte[(int) 10];
            if (r_entity.isStreaming()) {
                DataInputStream is = new DataInputStream(r_entity.getContent());
                is.readFully(result);
                // no load this result to webview
            }
            XMLResponseParser.writeXMLtoSDCard(result, "https_response.html");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

为什么你甚至要求用户选择两次选项?完全跳过步骤1

答案 1 :(得分:0)

private final String URL_STRING = "https://www.paypal.com/checkout/";

    public void postData() {
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
            nameValuePairs.add(new BasicNameValuePair("param2", "value2"));

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL_STRING);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(httppost);
            String data = new BasicResponseHandler().handleResponse(response);            

            mWebView.loadDataWithBaseURL(httppost.getURI().toString(), data, "text/html", HTTP.UTF_8, null);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }