请使用socket相关函数(非curl)实现如下功能:构造一个post请求,发送到指定httpserver的指定端口的指定请求路径(如http://www.example.com:8080/test)

用户名(username):温柔一刀

       密码(pwd):&123=321&321=123&

 个人简介(intro):Hello world!


且该http server需要以下cookie来进行简单的用户动作跟踪:

cur_query:you&me

  last_tm:...(上次请求的unix时间戳,定为当前请求时间前10分钟)

   cur_tm:...(当前请求的unix时间戳)

设置超时为10秒,发出请求后,将http server的响应内容输出(腾讯)


<?php
    if($fp = fsockopen('localhost','80')){
        //连接成功
        $request_data="POST/2.php HTTP/1.1"."\r\n";//请求行
        $request_data.="Host:localhost"."\r\n";//host头信息
        $request_data.="User-Agent:Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0)"."\r\n";//host头信息

        $request_content="username=".urlencode('温柔一刀')
        ."&pwd=".urlencode('&123=321&321=123&')
        ."&intro=".urlencode("Hello world!");

        $request_data.="Content-Type:application/x-www-form-urlencoded"."\r\n";
        $request_data.="Content-Length:".strlen($request_content)."\r\n";
        $cur_query=urlencode("you&me");

        $last_tm=time()-10*60;
        $cur_tm=time();
        $request_data.="Cookie:cur_query=$cur_query;last_tm=$last_tm;cur_tm=$cur_tm"."\r\n";
        $request_data.="\r\n";//请求头信息结束时的空行

        //请求主体数据部分
        $request_data.=$request_content;
        //利用建立好的通道,将数据发送过去
        fwrite($fp,$request_data);//写入数据(发送数据)

        //读取数据
        while(!feof($fp)){//判断是否没有到文件末尾
            $resp_data=fgets($fp);
            echo$resp_data.'<br>';
        }
    }
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不负好时光1001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值