您的位置:首页 > 理论基础 > 计算机网络

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

2017-10-11 21:37 826 查看
用户名(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>';
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐