简体   繁体   English

Java HttpPost到php页面

[英]Java HttpPost to php page

I'm trying to call a php page from Java code, since i have a long string i have to use the POST method. 我正在尝试从Java代码调用php页面,因为我的字符串很长,因此必须使用POST方法。 I tried the following code: 我尝试了以下代码:

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();

and also: 并且:

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();

Both get the php page, but when i call $_POST or $_REQUEST they are empty array. 两者都获取php页面,但是当我调用$_POST$_REQUEST它们是空数组。 If i use a HttGet i find the passed values in $_GET ... I guess the problem is setting the params in the right manner to the Post....but how? 如果我使用HttGet,我会在$_GET找到传递的值...我想问题是对Post设置了正确的参数。

Solved. 解决了。 I don't know why, but it works only when i remove the www from the URL ... 我不知道为什么,但是只有当我从URL删除www时,它才起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM