0

我需要能够在http:///Default.aspx上发布表单。我已经尝试过我认为我认为需要传递的所有不同可能的参数组合,但我没有取得任何成功。

我希望能够通过 android 代码发布,但除此之外,我觉得这是我为发布的请求提出的问题。这是我尝试发布表单的代码:

private void Post() {
    // TODO Auto-generated method stub
    // Create a new HttpClient and Post Header

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://customer.chuckwilson.com/Default.aspx");
    httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0");
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httppost.setHeader("Accept-Charset", "utf-8");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("__VIEWSTATE", "<state value>"));
        nameValuePairs.add(new BasicNameValuePair("__EVENTVALIDATION", "<Event validation value>"));

        nameValuePairs.add(new BasicNameValuePair("txtEmailAddress", "email@android.com"));

        nameValuePairs.add(new BasicNameValuePair("txtStreetAddress", "streetandroid"));
        nameValuePairs.add(new BasicNameValuePair("txtZipCode", "5454"));
        nameValuePairs.add(new BasicNameValuePair("txtCity", "cityandroid"));
        nameValuePairs.add(new BasicNameValuePair("CallBack", "rdCallBackYes"));
        nameValuePairs.add(new BasicNameValuePair("txtLastName", "lastandroid"));
        nameValuePairs.add(new BasicNameValuePair("phone", "(111) 111-1111"));

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

每次我运行代码时,我得到的响应都是该表单的 html。这表明我的参数是错误的,但我真的看不出有什么问题。我希望有人能指出错误。任何帮助深表感谢。

4

1 回答 1

0

The form in the page you link to is defined with this HTML tag:

<form name="form1" method="post" action="Default.aspx" id="form1" enctype="multipart/form-data">

The "enctype" means it is not using the URL-Encoded form submission that you are producing, but the MIME multipart protocol that's normally used for file uploads.

See this blog article, which shows how to use this type of form: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

于 2012-04-12T09:27:11.550 回答