浏览器发送 POST 请求

表单 form.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <form action="a.php" method="post">
  9. username: <input type="text" name="username">
  10. password: <input type="password" name="password">
  11. <input type="submit" value="提交">
  12. </form>
  13. </body>
  14. </html>

a.php 接收数据

  1. <?php
  2. print_r($_POST);

    

HTTP POST 的请求和响应

telnet 模拟 POST 请求

  1. POST /php/http/a.php HTTP/1.1
  2. Host: 127.0.0.17
  3. Content-type: application/x-www-form-urlencoded
  4. Content-length: 28
  5.  
  6. username=dee&password=123456HTTP/1.1 200 OK
  7. Date: Sat, 11 Jul 2015 17:05:31 GMT
  8. Server: Apache/2.2.21 (Win32) PHP/5.3.10
  9. X-Powered-By: PHP/5.3.10
  10. Content-Length: 57
  11. Content-Type: text/html
  12.  
  13. Array
  14. (
  15. [username] => dee
  16. [password] => 123456
  17. )

  

http 发送类 http.calss.php 补全 POST 请求

测试打印拼接的 POST  请求的请求行、头信息、主体信息,代码:

  1. <?php
  2. /*
  3. PHP + socket 编程
  4. @发送 HTTP 请求
  5. @模拟下载
  6. @实现注册、登录、批量发帖
  7. */
  8.  
  9. //http 请求类的接口
  10. interface Proto{
  11. //连接 url
  12. function conn($url);
  13.  
  14. //发送 GET 请求
  15. function get();
  16.  
  17. //发送 POST 请求
  18. function post();
  19.  
  20. //关闭连接
  21. function close();
  22. }
  23.  
  24. class Http implements Proto{
  25.  
  26. //换行符
  27. const CRLF = "\r\n";
  28.  
  29. //fsocket 的错误号与错误描述
  30. protected $errno = -1;
  31. protected $errstr = '';
  32.  
  33. //响应内容
  34. protected $response = '';
  35.  
  36. protected $url = null;
  37. protected $version = 'HTTP/1.1';
  38. protected $fh = null;
  39.  
  40. protected $line = array();
  41. protected $header = array();
  42. protected $body = array();
  43.  
  44. public function __construct($url){
  45. $this->conn($url);
  46. $this->setHeader('Host:' . $this->url['host']);
  47. }
  48.  
  49. //写请求行
  50. protected function setLine($method){
  51. $this->line[0] = $method . ' ' . $this->url['path'] . ' ' . $this->version;
  52. }
  53.  
  54. //写头信息
  55. protected function setHeader($headerline){
  56. $this->header[] = $headerline;
  57. }
  58.  
  59. //写主体信息
  60. protected function setBody($body){
  61. //构造 body 的字符串
  62. $this->body[] = http_build_query($body);
  63. }
  64.  
  65. //连接 url
  66. public function conn($url){
  67. $this->url = parse_url($url);
  68. //判断端口
  69. if(!isset($this->url['port'])){
  70. $this->url['port'] = 80;
  71. }
  72. $this->fh = fsockopen($this->url['host'], $this->url['port'], $this->errno, $this->errstr, 3);
  73. }
  74.  
  75. //构造 GET 请求的数据
  76. public function get(){
  77. $this->setLine('GET');
  78. //发送请求
  79. $this->request();
  80. return $this->response;
  81. }
  82.  
  83. //构造 POST 请求的数据
  84. public function post($body = array()){
  85. //构造请求行
  86. $this->setLine('POST');
  87.  
  88. //设置 Content-type 和 Content-length
  89. $this->setHeader('Content-type: application/x-www-form-urlencoded');
  90.  
  91. //构造主体信息, 和 GET 请求不一样的地方
  92. $this->setBody($body);
  93.  
  94. $this->setHeader('Content-length: ' . strlen($this->body[0]));
  95.  
  96. //发送请求
  97. $this->request();
  98. return $this->response;
  99. }
  100.  
  101. //发送请求
  102. public function request(){
  103. //把请求行、头信息、主体信息拼接起来
  104. $req = array_merge($this->line, $this->header, array(''), $this->body, array(''));
  105. $req = implode(self::CRLF, $req);
  106. echo $req;exit;
  107.  
  108. fwrite($this->fh, $req);
  109.  
  110. while(!feof($this->fh)){
  111. $this->response .= fread($this->fh, 1024);
  112. }
  113.  
  114. //关闭连接
  115. $this->close();
  116. }
  117.  
  118. //关闭连接
  119. public function close(){
  120. fclose($this->fh);
  121. }
  122. }
  123.  
  124. set_time_limit(0);
  125.  
  126. //$url = 'http://book.douban.com/subject/26376603/';
  127. $url = 'http://127.0.0.17/php/http/a.php';
  128.  
  129. $http = new Http($url);
  130. //echo $http->get();
  131.  
  132. //打乱
  133. $str = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
  134. $username = substr($str, 0, 5);
  135. $password = substr($str, 6, 12);
  136.  
  137. $body = array(
  138. 'username'=>$username,
  139. 'password'=>$password
  140. );
  141. $http->post($body);
  142.  
  143. echo $username,'--',$password;

输出:

  

然后发送请求,同时配合 for 循环就可以进行(不注册且没有验证码的)批量注册(把 username 换成 title,password 换成 content,可以使用 php 的命令行工具 ,在 cmd 中执行 php.exe 文件路径)。

使用命令行如下所示:

完整代码:

  1. <?php
  2. /*
  3. PHP + socket 编程
  4. @发送 HTTP 请求
  5. @模拟下载
  6. @实现注册、登录、批量发帖
  7. */
  8.  
  9. //http 请求类的接口
  10. interface Proto{
  11. //连接 url
  12. function conn($url);
  13.  
  14. //发送 GET 请求
  15. function get();
  16.  
  17. //发送 POST 请求
  18. function post();
  19.  
  20. //关闭连接
  21. function close();
  22. }
  23.  
  24. class Http implements Proto{
  25.  
  26. //换行符
  27. const CRLF = "\r\n";
  28.  
  29. //fsocket 的错误号与错误描述
  30. protected $errno = -1;
  31. protected $errstr = '';
  32.  
  33. //响应内容
  34. protected $response = '';
  35.  
  36. protected $url = null;
  37. protected $version = 'HTTP/1.1';
  38. protected $fh = null;
  39.  
  40. protected $line = array();
  41. protected $header = array();
  42. protected $body = array();
  43.  
  44. public function __construct($url){
  45. $this->conn($url);
  46. $this->setHeader('Host:' . $this->url['host']);
  47. }
  48.  
  49. //写请求行
  50. protected function setLine($method){
  51. $this->line[0] = $method . ' ' . $this->url['path'] . ' ' . $this->version;
  52. }
  53.  
  54. //写头信息
  55. protected function setHeader($headerline){
  56. $this->header[] = $headerline;
  57. }
  58.  
  59. //写主体信息
  60. protected function setBody($body){
  61. //构造 body 的字符串
  62. $this->body[] = http_build_query($body);
  63. }
  64.  
  65. //连接 url
  66. public function conn($url){
  67. $this->url = parse_url($url);
  68. //判断端口
  69. if(!isset($this->url['port'])){
  70. $this->url['port'] = 80;
  71. }
  72. $this->fh = fsockopen($this->url['host'], $this->url['port'], $this->errno, $this->errstr, 3);
  73. }
  74.  
  75. //构造 GET 请求的数据
  76. public function get(){
  77. $this->setLine('GET');
  78. //发送请求
  79. $this->request();
  80. return $this->response;
  81. }
  82.  
  83. //构造 POST 请求的数据
  84. public function post($body = array()){
  85. //构造请求行
  86. $this->setLine('POST');
  87.  
  88. //设置 Content-type 和 Content-length
  89. $this->setHeader('Content-type: application/x-www-form-urlencoded');
  90.  
  91. //构造主体信息, 和 GET 请求不一样的地方
  92. $this->setBody($body);
  93.  
  94. $this->setHeader('Content-length: ' . strlen($this->body[0]));
  95.  
  96. //发送请求
  97. $this->request();
  98. return $this->response;
  99. }
  100.  
  101. //发送请求
  102. public function request(){
  103. //把请求行、头信息、主体信息拼接起来
  104. $req = array_merge($this->line, $this->header, array(''), $this->body, array(''));
  105. $req = implode(self::CRLF, $req);
  106. //echo $req;exit;
  107.  
  108. fwrite($this->fh, $req);
  109.  
  110. while(!feof($this->fh)){
  111. $this->response .= fread($this->fh, 1024);
  112. }
  113.  
  114. //关闭连接
  115. $this->close();
  116. }
  117.  
  118. //关闭连接
  119. public function close(){
  120. fclose($this->fh);
  121. }
  122. }
  123.  
  124. set_time_limit(0);
  125.  
  126. //$url = 'http://book.douban.com/subject/26376603/';
  127. $url = 'http://127.0.0.17/php/http/a.php';
  128.  
  129. $http = new Http($url);
  130. //echo $http->get();
  131.  
  132. //批量
  133. for($i = 1; $i < 100; $i++){
  134.  
  135. //打乱
  136. $str = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
  137. $username = substr($str, 0, 5);
  138. $password = substr($str, 6, 12);
  139.  
  140. $body = array(
  141. 'username'=>$username,
  142. 'password'=>$password
  143. );
  144. $http->post($body);
  145.  
  146. echo $username,'--',$password;
  147.  
  148. usleep(2000);
  149. }

  

HTTP 笔记与总结(4 )socket 编程:批量发帖的更多相关文章

  1. 03 http+socket编程批量发帖

    <?php // http请求类的接口 interface Proto { // 连接url function conn($url); //发送get查询 function get(); // ...

  2. HTTP 笔记与总结(5)socket 编程:使用 HTTP 协议模拟登录并发帖

    在 VeryCD 上注册两个帐号,发送和接收站内信,观察 POST 请求时发送的参数(h****2 发送给 d***2).(最好用 FireFox 的 FireBug 工具,发送站内信之前选中 “保持 ...

  3. C# Socket编程笔记(转)

    C# Socket编程笔记 http://www.cnblogs.com/stg609/archive/2008/11/15/1333889.html TCP Socket:Server 端连接步骤: ...

  4. 转 网络编程学习笔记一:Socket编程

    题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...

  5. JAVA Socket 编程学习笔记(二)

    在上一篇中,使用了 java Socket+Tcp/IP  协议来实现应用程序或客户端--服务器间的实时双向通信,本篇中,将使用 UDP 协议来实现 Socket 的通信. 1. 关于UDP UDP协 ...

  6. JAVA Socket 编程学习笔记(一)

    1. Socket 通信简介及模型 Java Socket 可实现客户端--服务器间的双向实时通信.java.net包中定义的两个类socket和ServerSocket,分别用来实现双向连接的cli ...

  7. HTTP 笔记与总结(3 )socket 编程:发送 GET 请求

    使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...

  8. LInux下socket编程学习笔记

    1.socket套接字: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...

  9. Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 & Socket编程基础)

    FTP作业 本节内容: 面向对象高级语法部分 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 参考:http://www.cnblogs.com/wupeiqi/ ...

随机推荐

  1. 信与信封问题(codevs 1222)

    题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...

  2. 【读书笔记】读《JavaScript DOM 编程艺术-第2版》

    1.DHTML DHTML曾被认为是HTML/XHTML.CSS和JavaScript相结合的产物,就像今天的HTML5那样,但把这些东西真正凝聚在一起的是DOM.如果真的需要来描述这一过程的话,“D ...

  3. js iframe onload &line-height浏览器兼容问题

    1.IE iframe onload事件 在IE下给iframe添加onload事件经常无效,因为在IE下它最多只能被激活一次,而且无论你有多少个iframe,被激活的也只能是最后一个的.可以用下面的 ...

  4. ASP.NET MVC 3 使用Model自定义验证的样式

    1.修改jquery.validate.unobtrusive.js 将onError方法修改 //修改的部分 //////////////////////////////////////////// ...

  5. C# Winform ListView使用

    以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...

  6. NDK编译生成so文件

    1 首先加载项目

  7. 在crontab中动态写日志

    45 3 * * * setsid script -c /home/dlht/shell/coreBusiness/coreOpt.sh  >> /home/dlht/logs/coreO ...

  8. LoadRunner中取Request、Response

    LoadRunner中取Request.Response LoadRunner两个“内置变量”: 1.REQUEST,用于提取完整的请求头信息. 2.RESPONSE,用于提取完整的响应头信息. 响应 ...

  9. python的包和模块

    python 的包即文件夹,但是必须包含_init_.py 模块就是xx.py

  10. Seismic Unix的一些历史

    本文是我从官网上拷贝过来的,上国外网越来越慢了……(离题了). At the Society of Exploration Geophysicists (SEG) Annual Meeting in ...