HTTPS Post表单Android

时间:2011-06-15 06:52:49

标签: java android

所以我为此进行了广泛的搜索,使用了基本相同的代码的所有版本,为此目的浮出水面。

我真的不知道这里有什么不对,我找不到原因。无论如何这是我的代码。

根据我的理解,如果phoneNumber和PIN是正确的......我应该返回“我的帐户”页面,但事实并非如此,它只会再次返回同一页面。

任何帮助将不胜感激!

private void PostIt() {
    // TODO Auto-generated method stub
    // Create a new HttpClient and Post Header
  String phoneNumber = "1234567890";
  String PIN = "1234";
  HttpParams params = new BasicHttpParams();
  params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("https://care.windmobile.ca/signin.aspx?ReturnUrl=%2fMy_Account%2fDefault.aspx");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE", "/wEPDwUKMTk2NDUwMTE2Mg9kFgJmD2QWAgIDD2QWAgIBD2QWBmYPZBYCAgMPFgIeB1Zpc2libGVnFgJmD2QWBAIFDw9kFgIeCm9ua2V5cHJlc3MFS2phdmFzY3JpcHQ6c3VibWl0aXQoZXZlbnQsJ2N0bDAwX21pbmlDYXJ0X1JlZmVyZW5jZU51bWJlckxvYWQxX2J0blN1Ym1pdCcpO2QCBw8PFgIfAGhkZAICD2QWAmYPDxYCHgRUZXh0BQtDYXJ0OiBFbXB0eWRkAgcPZBYCAgEPZBYEZg9kFgQCAg8WAh4Fc3R5bGUFDURpc3BsYXk6bm9uZTtkAg4PDxYCHwJlZGQCAQ9kFgICCQ8PFgIfAGhkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WBAUqY3RsMDAkbWluaUNhcnQkUmVmZXJlbmNlTnVtYmVyTG9hZDEkYnRuWWVzBRVjdGwwMCRtaW5pQ2FydCRidG5ZZXMFMWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlck1haW4kTG9naW4xJGNoa1JlbWVtYmVyTWUFL2N0bDAwJENvbnRlbnRQbGFjZUhvbGRlck1haW4kTG9naW4xJGltZ0J0bkxvZ2lu23ia8lDQTxfwbvp2fzA6vVPrS8A="));
            nameValuePairs.add(new BasicNameValuePair("__EVENTVALIDATION", "/wEWCwLUx9OsBgKFrrDxDwKAr5ffDwLSrYKxDAKI7JvbCQLnu7OCBQLcrt2bBQLKz4HaAwKm6dz6BQLrwcNyAti18pQOTyeCXHQvJm+6RwcaA+OYx3TE7Ec="));
            nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$imgBtnLogin.x","53"));
            nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$imgBtnLogin.y","21"));
            nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$txtMSISDN", phoneNumber));
            nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$txtPIN", PIN));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            Log.d(getPackageName(), "executed http post req");
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {    
                Log.i("RESPONSE",EntityUtils.toString(resEntity));
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            Log.e(getPackageName(), "error1 in req");
            e.printStackTrace();
        } catch (IOException e) {
                    // TODO Auto-generated catch block
            Log.e(getPackageName(), "error2 in req");
            e.printStackTrace();
        }
}

1 个答案:

答案 0 :(得分:1)

[vince]在#android-dev(FreeNode)上回答,再添加两个BasicNameValuePairs

nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$imgBtnLogin.x","53")); nameValuePairs.add(new BasicNameValuePair("ctl00$ContentPlaceHolderMain$Login1$imgBtnLogin.y","21"));

瞧,瞧!它有效!