• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
车车大人
博客园    首页    新随笔    联系   管理     

telnet运用http协议+罕见的表单提交方式

telnet模拟http请求:

cmd进去以后输入:telnet 127.0.0.1 80 回车(如果程序用不了,那么进去控制面板->程序和功能->打开或关闭windows功能,勾选telnet客户端即可),然后按下ctrl+],回车就可以了

-------------------------

GET请求:

GET http://localhost/test.php HTTP/1.1

HOST:localhost

 

注意要多一行空格

 

------------------------

接下来是POST请求:

 


POST http://localhost/test.php HTTP/1.1
HOST:localhost
Content-type:application/x-www-form-urlencoded
content-length:20

act=query&name=zzz 

 

--------------------------

 

file_get_contents模拟表单提交(这里是post提交)

<?php
    
    $postData = array(
        'title'=> '我是file_get_contents的构造数据',
        'content'=> '我是file_get_contents的构造数据内容',
        'publish'=> '发布',
    );

    $postData = http_build_query($postData);

    $opts = array(
      'http'=>array(
        'method'=>"POST",
        'header'=>"Host:localhost\r\n" .
                  "Content-type:application/x-www-form-urlencoded\r\n" .
                  "Content-length:" . strlen($postData) . "\r\n",
        'content'=>$postData,
      )
    );

    $context = stream_context_create($opts);
    file_get_contents("http://localhost/http/index.php",false,$context);

 

fopen模拟表单提交

<?php
    
    $postData = array(
        'title'=> '我是fopen的构造数据',
        'content'=> '我是fopen的构造数据内容',
        'publish'=> '发布',
    );

    $postData = http_build_query($postData);

    $opts = array(
      'http'=>array(
        'method'=>"POST",
        'header'=>"Host:localhost\r\n" .
                  "Content-type:application/x-www-form-urlencoded\r\n" .
                  "Content-length:" . strlen($postData) . "\r\n",
        'content'=>$postData,
      )
    );

    $context = stream_context_create($opts);
    $fp = fopen("http://localhost/http/index.php","r",false,$context);
        $fclose($fp);

 

curl方式模拟表单提交

$url = "http://localhost/http/index.php";
    $postData = array(
        'title'=> '我是curl的构造数据',
        'content'=> '我是curl的构造数据内容',
        'publish'=> '发布',
    );
    //初始化一个curl会话
    $ch = curl_init();
    //设置相应的会话选项
    curl_setopt($ch, CURLOPT_URL, $url);//设置提交网址
    curl_setopt($ch, CURLOPT_POST, 1); //设置提交方式,值为1表示肯定
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); //设置提交数据
    $output = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //提交成功后把数据返回成字符串
    //执行
    curl_exec($ch);
    //关闭
    curl_close($ch);

    echo $output;

 

socket模拟表单提交(看测试效果好像执行会比较慢在这里标注一下)

$postData = array(
        'title'=> '我是socket的构造数据',
        'content'=> '我是socket的构造数据内容',
        'publish'=> '发布',
    );

    $postData = http_build_query($postData);

    $fp = fsockopen("localhost",80,$errno,$errorStr,5);//主机名地址,端口号,错误码,错误信息,连接的时限
    $request = "POST http://localhost/http/index.php HTTP/1.1\r\n";
    $request .= "Host:localhost\r\n";
    $request .= "Content-type:application/x-www-form-urlencoded\r\n";
    $request .= "Content-length:".strlen($postData)."\r\n\r\n";
    $request .= $postData;
    fwrite($fp, $request);
    //如果你想把请求成功之后的数据读出来的话这么写
    while ( !feof($fp)) {
        echo fgets($fp,1024);//http内容信息
    }
    fclose($fp);

 

通往牛逼的路上,在意的只有远方!
posted @ 2017-10-13 18:03  车车大人  阅读(352)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3