douzhanlai4671 2015-03-14 22:02
浏览 126

JAVA - POST到PHP,没有收到变量

So I'm trying to post an IP from java to a php file on the web. The php file will then save it. Though the php file is not receiving the ip.

Java Code:

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 Code(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 . ':');
    }
}
?>

Does anybody have any clue as to why this is happening? I appreciate all and any help. Sorry if it's the PHP code, fairly new to PHP.

  • 写回答

1条回答 默认 最新

  • doulao1934 2015-03-15 03:05
    关注
    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();
        }
    }
    

    Source: http://hc.apache.org/

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真