首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过POST将参数发送到php文件并读取php文件的回波

通过POST将参数发送到php文件并读取php文件的回波
EN

Stack Overflow用户
提问于 2014-04-27 20:47:11
回答 1查看 804关注 0票数 1

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

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

到目前为止,这就是我所拥有的:

代码语言:javascript
复制
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参数的值呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-27 20:50:23

您可以这样阅读post回复:

代码语言:javascript
复制
    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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23328796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档