我试图从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
时才有效…