[英]JAVA - POST to PHP, Not Receiving Variables
因此,我正在尝试将IP从Java发布到网络上的php文件。 然后,PHP文件将保存它。 虽然PHP文件没有收到IP。
Java代码:
try {
// open a connection to the site
URL url = new URL("http://example.com/useranalytics/join.php");
URLConnection con = url.openConnection();
// activate the output
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
// send your parameters to your site
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
ps.print("ip=1.0.1.0");
ps.print("time=" + dateFormat.format(date));
// we have to get the input stream in order to actually send the request
con.getInputStream();
// close the print stream
ps.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
PHP代码(join.php):
<?php
foreach ($_POST as $key => $value) {
switch ($key) {
case 'ip':
$ip = $value;
break;
case 'time':
$time = $value;
break;
default:
break;
}
$ipf = "ipss.txt";
$handleip = fopen($ipf, "r");
$ips = fread($handleip, filesize($ipf));
if (strpos($ips,':' . $ip . ':') !== false) {
} else {
file_put_contents('ipss.txt', $ips . PHP_EOL . ':' . $IP . ':');
}
}
?>
有人对为什么发生有任何线索吗? 我感谢所有帮助。 抱歉,这是PHP代码,对PHP来说还很新。
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
// do something useful
} finally {
instream.close();
}
}
资料来源: http : //hc.apache.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.