【问题标题】:Sending parameters via POST to php file and reading php file's echo通过 POST 向 php 文件发送参数并读取 php 文件的回显
【发布时间】:2014-04-27 20:47:11
【问题描述】:

我将 NameValuePair 参数发送到我服务器上的一个 php 文件,这个 php 文件回显三个字符串值之一。

我需要用 Java 编写代码通过 POST 将这些参数发送到 PHP 文件,并将 php 文件的回显响应保存到字符串。

这是我目前所拥有的:

public String getStringFromUrl(String url, List<NameValuePair> params) throws IOException {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        HttpPost httpPost = new HttpPost(url);
        if (params != null) {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
        }
        httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        String response2 = (String) response;
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

我知道我有正确的代码行来发送 POST 参数,但是如何读取 php 文件回显的与给定 POST 参数对应的值?

【问题讨论】:

    标签: java php post echo


    【解决方案1】:

    您可以这样阅读帖子回复:

        HttpResponse response = client.execute(post);
        System.out.println("Response Code : " 
                    + response.getStatusLine().getStatusCode());
    
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));
    
        StringBuffer result = new StringBuffer();
        String line = "";
    

    在此处查看完整示例:

    http://www.mkyong.com/java/apache-httpclient-examples/

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 2021-05-03
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多