http帖子没有通过,我不认为我错过了一个参数

时间:2012-04-12 08:49:36

标签: android html forms http-post

我需要能够在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。这表明我的参数是错误的,但我真的没有看到它有什么问题。我希望有人可以指出错误。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您链接到的页面中的表单是使用此HTML标记定义的:

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

“enctype”表示它不使用您正在生成的URL-Encoded表单提交,而是使用通常用于文件上载的MIME多部分协议。

请参阅此博客文章,该文章介绍了如何使用此类表单:http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/