请使用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>';
}
}
?>
密码(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>';
}
}
?>
相关文章推荐
- 使用HttpClient发送HttpPost请求包含上传本地图片和远程图片的传输实现
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(2)----Httpclient使用详解
- PHP使用curl请求实现post方式上传图片文件功能示例
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- 调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。的解决方案
- Getting Started Spidering a Site使用Chilkat(python)练习的一个爬虫(from :http://www.example-code.com)
- PHP中使用CURL发送get/post请求上传图片批处理功能
- http以post方式上传一个文件,构造其请求头和消息报文 application/x-www-form-urlencoded multipart/form-data
- 使用JS实现倒计时功能(http://www.cnblogs.com/doll-net)
- 使用JS实现倒计时功能(http://www.cnblogs.com/doll-net)
- C#使用命令行方式实现Ping简单功能 http://www.cnblogs.com/kevinton/archive/2007/06/28/798581.html
- HTTP协议理解 + PHP使用curl函数发送Post请求的注意事项
- java中使用scoket模拟http post请求发送图片或文件
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 使用HttpWebRequest发送自定义POST请求(转帖)
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 使用java代码发送HTTP Post请求