【问题标题】:On Android, make a POST request with URL Encoded Form data without using UrlEncodedFormEntity在 Android 上,使用 URL 编码的表单数据发出 POST 请求,而不使用 UrlEncodedFormEntity
【发布时间】:2011-05-18 20:10:07
【问题描述】:

我有一个字符串,它已经是正确的 URLEncoded Form 格式,并希望通过 Android 上的 POST 请求将其发送到 PHP 服务器。我知道在 Android 上发送 URL 编码表单的方法使用UrlEncodedFormEntity,我知道how to use it。这样做的问题是数据进入了已经被 URL 编码并由 & 符号连接的函数,因此使用 UrlEncodedFormEntity 将需要大量额外的工作才能将其转换为 NameValuePairsList 而我宁愿不这样做.

那么,如何发出正确的 POST 请求,将此字符串作为内容正文发送?

我已经尝试过使用StringEntity,但是PHP服务器没有得到任何数据(空的$_POST对象)。

我正在测试 http://test.lifewanted.com/echo.json.php,这就是

<?php echo json_encode( $_REQUEST );

以下是已编码数据的示例:

partnerUserID=email%40example.com&partnerUserSecret=mypassword&command=Authenticate

【问题讨论】:

    标签: android urlencode http-post


    【解决方案1】:

    如果您不介意使用HttpURLConnection 而不是(推荐的)HttpClient,那么您可以这样做:

    public void performPost(String encodedData) {
        HttpURLConnection urlc = null;
        OutputStreamWriter out = null;
        DataOutputStream dataout = null;
        BufferedReader in = null;
        try {
            URL url = new URL(URL_LOGIN_SUBMIT);
            urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestMethod("POST");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            urlc.setUseCaches(false);
            urlc.setAllowUserInteraction(false);
            urlc.setRequestProperty(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
            urlc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            dataout = new DataOutputStream(urlc.getOutputStream());
            // perform POST operation
            dataout.writeBytes(encodedData);
            int responseCode = urlc.getResponseCode();
            in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
            String response;
            // write html to System.out for debug
            while ((response = in.readLine()) != null) {
                System.out.println(response);
            }
            in.close();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    【讨论】:

    • 好答案。在 Gingerbread 及以后, HttpURLConnection 是要走的路。考虑弃用 Apache HttpClient。
    • 我才意识到我从来没有接受这个作为答案。这对我很有用。谢谢!
    • 两件事:InputStreamReader,根据 Android 的文档 (developer.android.com/reference/java/io/InputStreamReader.html) 已经缓冲,因此不需要 BufferedReader。而且,根据 Android 的文档,在使用结束时,您应该(或者更确切地说必须)在 HttpURLConnection 上调用 disconnect() 以释放资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2019-10-07
    • 1970-01-01
    相关资源
    最近更新 更多