HTTP 笔记与总结(4 )socket 编程:批量发帖
浏览器发送 POST 请求:
表单 form.html
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <form action="a.php" method="post">
- username: <input type="text" name="username">
- password: <input type="password" name="password">
- <input type="submit" value="提交">
- </form>
- </body>
- </html>
a.php 接收数据
- <?php
- print_r($_POST);
HTTP POST 的请求和响应
telnet 模拟 POST 请求:
- POST /php/http/a.php HTTP/1.1
- Host: 127.0.0.17
- Content-type: application/x-www-form-urlencoded
- Content-length: 28
- username=dee&password=123456HTTP/1.1 200 OK
- Date: Sat, 11 Jul 2015 17:05:31 GMT
- Server: Apache/2.2.21 (Win32) PHP/5.3.10
- X-Powered-By: PHP/5.3.10
- Content-Length: 57
- Content-Type: text/html
- Array
- (
- [username] => dee
- [password] => 123456
- )
http 发送类 http.calss.php 补全 POST 请求:
测试打印拼接的 POST 请求的请求行、头信息、主体信息,代码:
- <?php
- /*
- PHP + socket 编程
- @发送 HTTP 请求
- @模拟下载
- @实现注册、登录、批量发帖
- */
- //http 请求类的接口
- interface Proto{
- //连接 url
- function conn($url);
- //发送 GET 请求
- function get();
- //发送 POST 请求
- function post();
- //关闭连接
- function close();
- }
- class Http implements Proto{
- //换行符
- const CRLF = "\r\n";
- //fsocket 的错误号与错误描述
- protected $errno = -1;
- protected $errstr = '';
- //响应内容
- protected $response = '';
- protected $url = null;
- protected $version = 'HTTP/1.1';
- protected $fh = null;
- protected $line = array();
- protected $header = array();
- protected $body = array();
- public function __construct($url){
- $this->conn($url);
- $this->setHeader('Host:' . $this->url['host']);
- }
- //写请求行
- protected function setLine($method){
- $this->line[0] = $method . ' ' . $this->url['path'] . ' ' . $this->version;
- }
- //写头信息
- protected function setHeader($headerline){
- $this->header[] = $headerline;
- }
- //写主体信息
- protected function setBody($body){
- //构造 body 的字符串
- $this->body[] = http_build_query($body);
- }
- //连接 url
- public function conn($url){
- $this->url = parse_url($url);
- //判断端口
- if(!isset($this->url['port'])){
- $this->url['port'] = 80;
- }
- $this->fh = fsockopen($this->url['host'], $this->url['port'], $this->errno, $this->errstr, 3);
- }
- //构造 GET 请求的数据
- public function get(){
- $this->setLine('GET');
- //发送请求
- $this->request();
- return $this->response;
- }
- //构造 POST 请求的数据
- public function post($body = array()){
- //构造请求行
- $this->setLine('POST');
- //设置 Content-type 和 Content-length
- $this->setHeader('Content-type: application/x-www-form-urlencoded');
- //构造主体信息, 和 GET 请求不一样的地方
- $this->setBody($body);
- $this->setHeader('Content-length: ' . strlen($this->body[0]));
- //发送请求
- $this->request();
- return $this->response;
- }
- //发送请求
- public function request(){
- //把请求行、头信息、主体信息拼接起来
- $req = array_merge($this->line, $this->header, array(''), $this->body, array(''));
- $req = implode(self::CRLF, $req);
- echo $req;exit;
- fwrite($this->fh, $req);
- while(!feof($this->fh)){
- $this->response .= fread($this->fh, 1024);
- }
- //关闭连接
- $this->close();
- }
- //关闭连接
- public function close(){
- fclose($this->fh);
- }
- }
- set_time_limit(0);
- //$url = 'http://book.douban.com/subject/26376603/';
- $url = 'http://127.0.0.17/php/http/a.php';
- $http = new Http($url);
- //echo $http->get();
- //打乱
- $str = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
- $username = substr($str, 0, 5);
- $password = substr($str, 6, 12);
- $body = array(
- 'username'=>$username,
- 'password'=>$password
- );
- $http->post($body);
- echo $username,'--',$password;
输出:
然后发送请求,同时配合 for 循环就可以进行(不注册且没有验证码的)批量注册(把 username 换成 title,password 换成 content,可以使用 php 的命令行工具 ,在 cmd 中执行 php.exe 文件路径)。
使用命令行如下所示:
完整代码:
- <?php
- /*
- PHP + socket 编程
- @发送 HTTP 请求
- @模拟下载
- @实现注册、登录、批量发帖
- */
- //http 请求类的接口
- interface Proto{
- //连接 url
- function conn($url);
- //发送 GET 请求
- function get();
- //发送 POST 请求
- function post();
- //关闭连接
- function close();
- }
- class Http implements Proto{
- //换行符
- const CRLF = "\r\n";
- //fsocket 的错误号与错误描述
- protected $errno = -1;
- protected $errstr = '';
- //响应内容
- protected $response = '';
- protected $url = null;
- protected $version = 'HTTP/1.1';
- protected $fh = null;
- protected $line = array();
- protected $header = array();
- protected $body = array();
- public function __construct($url){
- $this->conn($url);
- $this->setHeader('Host:' . $this->url['host']);
- }
- //写请求行
- protected function setLine($method){
- $this->line[0] = $method . ' ' . $this->url['path'] . ' ' . $this->version;
- }
- //写头信息
- protected function setHeader($headerline){
- $this->header[] = $headerline;
- }
- //写主体信息
- protected function setBody($body){
- //构造 body 的字符串
- $this->body[] = http_build_query($body);
- }
- //连接 url
- public function conn($url){
- $this->url = parse_url($url);
- //判断端口
- if(!isset($this->url['port'])){
- $this->url['port'] = 80;
- }
- $this->fh = fsockopen($this->url['host'], $this->url['port'], $this->errno, $this->errstr, 3);
- }
- //构造 GET 请求的数据
- public function get(){
- $this->setLine('GET');
- //发送请求
- $this->request();
- return $this->response;
- }
- //构造 POST 请求的数据
- public function post($body = array()){
- //构造请求行
- $this->setLine('POST');
- //设置 Content-type 和 Content-length
- $this->setHeader('Content-type: application/x-www-form-urlencoded');
- //构造主体信息, 和 GET 请求不一样的地方
- $this->setBody($body);
- $this->setHeader('Content-length: ' . strlen($this->body[0]));
- //发送请求
- $this->request();
- return $this->response;
- }
- //发送请求
- public function request(){
- //把请求行、头信息、主体信息拼接起来
- $req = array_merge($this->line, $this->header, array(''), $this->body, array(''));
- $req = implode(self::CRLF, $req);
- //echo $req;exit;
- fwrite($this->fh, $req);
- while(!feof($this->fh)){
- $this->response .= fread($this->fh, 1024);
- }
- //关闭连接
- $this->close();
- }
- //关闭连接
- public function close(){
- fclose($this->fh);
- }
- }
- set_time_limit(0);
- //$url = 'http://book.douban.com/subject/26376603/';
- $url = 'http://127.0.0.17/php/http/a.php';
- $http = new Http($url);
- //echo $http->get();
- //批量
- for($i = 1; $i < 100; $i++){
- //打乱
- $str = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
- $username = substr($str, 0, 5);
- $password = substr($str, 6, 12);
- $body = array(
- 'username'=>$username,
- 'password'=>$password
- );
- $http->post($body);
- echo $username,'--',$password;
- usleep(2000);
- }
HTTP 笔记与总结(4 )socket 编程:批量发帖的更多相关文章
- 03 http+socket编程批量发帖
<?php // http请求类的接口 interface Proto { // 连接url function conn($url); //发送get查询 function get(); // ...
- HTTP 笔记与总结(5)socket 编程:使用 HTTP 协议模拟登录并发帖
在 VeryCD 上注册两个帐号,发送和接收站内信,观察 POST 请求时发送的参数(h****2 发送给 d***2).(最好用 FireFox 的 FireBug 工具,发送站内信之前选中 “保持 ...
- C# Socket编程笔记(转)
C# Socket编程笔记 http://www.cnblogs.com/stg609/archive/2008/11/15/1333889.html TCP Socket:Server 端连接步骤: ...
- 转 网络编程学习笔记一:Socket编程
题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...
- JAVA Socket 编程学习笔记(二)
在上一篇中,使用了 java Socket+Tcp/IP 协议来实现应用程序或客户端--服务器间的实时双向通信,本篇中,将使用 UDP 协议来实现 Socket 的通信. 1. 关于UDP UDP协 ...
- JAVA Socket 编程学习笔记(一)
1. Socket 通信简介及模型 Java Socket 可实现客户端--服务器间的双向实时通信.java.net包中定义的两个类socket和ServerSocket,分别用来实现双向连接的cli ...
- HTTP 笔记与总结(3 )socket 编程:发送 GET 请求
使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...
- LInux下socket编程学习笔记
1.socket套接字: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...
- Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 & Socket编程基础)
FTP作业 本节内容: 面向对象高级语法部分 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 参考:http://www.cnblogs.com/wupeiqi/ ...
随机推荐
- 信与信封问题(codevs 1222)
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...
- 【读书笔记】读《JavaScript DOM 编程艺术-第2版》
1.DHTML DHTML曾被认为是HTML/XHTML.CSS和JavaScript相结合的产物,就像今天的HTML5那样,但把这些东西真正凝聚在一起的是DOM.如果真的需要来描述这一过程的话,“D ...
- js iframe onload &line-height浏览器兼容问题
1.IE iframe onload事件 在IE下给iframe添加onload事件经常无效,因为在IE下它最多只能被激活一次,而且无论你有多少个iframe,被激活的也只能是最后一个的.可以用下面的 ...
- ASP.NET MVC 3 使用Model自定义验证的样式
1.修改jquery.validate.unobtrusive.js 将onError方法修改 //修改的部分 //////////////////////////////////////////// ...
- C# Winform ListView使用
以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...
- NDK编译生成so文件
1 首先加载项目
- 在crontab中动态写日志
45 3 * * * setsid script -c /home/dlht/shell/coreBusiness/coreOpt.sh >> /home/dlht/logs/coreO ...
- LoadRunner中取Request、Response
LoadRunner中取Request.Response LoadRunner两个“内置变量”: 1.REQUEST,用于提取完整的请求头信息. 2.RESPONSE,用于提取完整的响应头信息. 响应 ...
- python的包和模块
python 的包即文件夹,但是必须包含_init_.py 模块就是xx.py
- Seismic Unix的一些历史
本文是我从官网上拷贝过来的,上国外网越来越慢了……(离题了). At the Society of Exploration Geophysicists (SEG) Annual Meeting in ...