Java HttpPost 到 php 页面

标签 java php post http-post

我正在尝试从 Java 代码调用 php 页面,因为我有一个很长的字符串,我必须使用 POST 方法。 我尝试了以下代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri[0]);
if (params != null){
    post.setEntity(new UrlEncodedFormEntity(params));
}
response = httpclient.execute(post);
StatusLine statusLine = response.getStatusLine();

还有:

HttpClient httpclient = new DefaultHttpClient();
URL obj = new URL(uri[0]);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
String urlParameters = "checkout=" + "dfdfdf" + "&location=" + "BOZEN";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer responsei = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    responsei.append(inputLine);
}
in.close();

两者都获取 php 页面,但是当我调用 $_POST$_REQUEST 时,它们是空数组。如果我使用 HttGet,我会在 $_GET 中找到传递的值... 我想问题是以正确的方式设置帖子的参数......但是如何设置?

最佳答案

已解决。我不知道为什么,但只有当我从 URL 中删除 www 时它才有效...

关于Java HttpPost 到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082967/

相关文章:

phpmailer 在发送电子邮件时打印一长串详细信息(客户端 -> 服务器)

php - 使用 Zend_Http_Client 发送 post 请求

IIS:发布到默认页面时出现 405(方法不允许)

Java:将常量存储在非方法接口(interface)或类中更合适吗?

java - 简写为[]

java - 将 foreach 转换为 lambda 表达式

java - 如何对 mongodb 中存储的元数据进行分类

php - 如何为像 X 这样的值更改\n 值

php - 如何使用 mysql db & slim 3 PHP 框架在同一个函数中执行 2 个查询

php - 拉维尔 : Load a view after posting