打開cmd運行框,輸入Telnet www.baidu.com 80後按回車鍵。此時看到的界面是黑屏狀態,標題欄顯示 Telnet www.baidu.com
若是提示telnet不是內部或外部命令
能夠參考百度經驗。利用快捷鍵打開「Ctrl+]
」打開Telnet回顯,按回車鍵後能夠進入編輯狀態。javascript
回顯模式:php
編輯模式:html
接下來是考驗手速的時候了,單身黨必定完勝的遊戲。輸入GET /index.html HTTP/1.1按回車鍵,接着輸入HOST:www.baidu.com,連續按兩次回車鍵(兩次回車表明提交請求)輸入速度不夠快的話,將會鏈接失敗。能夠將代碼寫入記事本,使用時直接複製就行了。java
而後來看看服務器的返回數據:json
Http.class.php數組
<?php class HttpRequest { private $_host; //主機地址 private $_port; //端口號 private $_requestHead; //請求頭信息 private $_requestMethod; //請求方式 private $_requestPath; //請求路徑 private $_requestContent = ''; //請求正文 private $_httpVersion = '1.1'; //協議版本 /** * [__contruction 初始化成員屬性] * @author:JieJie */ public function __construct($host,$port,$method,$path) { $this->_host = $host; $this->_port = $port; $this->_requestMethod = strtoupper($method); $this->_requestPath = $path; } /** * [setRequestHead 設置請求頭信息] * @author:JieJie * @DateTime:2016-11-25T00:22:04+0800 * @param [array] $data [頭信息數組] */ public function setRequestHead($data) { foreach ($data as $key => $value) { $this->_requestHead .= $key.':'.$value."\r\n"; } $this->_requestHead .= "\r\n"; return $this; } /** * [setRequestContent 設置請求正文] * @author:JieJie * @DateTime:2016-11-25T00:37:53+0800 * @param [array] $data */ public function setRequestContent($data) { if($this->_requestMethod != 'POST') return $this; foreach ($data as $key => $value) { $this->_requestContent .= "{$key}={$value}&"; } $this->_requestContent = rtrim($this->_requestContent,'&')."\r\n"; return $this; } /** * [sendRequest 發送HTTP請求並獲取結果] * @author:JieJie * @DateTime:2016-11-25T00:51:53+0800 */ public function sendRequest() { //拼接請求行 $http = "{$this->_requestMethod} {$this->_requestPath} HTTP/{$this->_httpVersion}\r\n"; //拼接請求頭 $http .= $this->_requestHead; //拼接請求正文 $http .= $this->_requestContent; //建立鏈接 $fp = fsockopen($this->_host,$this->_port,$errno,$errstr); if(!$fp) throw new Exception($errstr); //發送請求 fwrite($fp, $http); //得到結果 $result = ''; while (!feof($fp)) $result .= fgets($fp); return $result; } } ?>
client.php服務器
<?php include './Http.class.php'; $Http = new HttpRequest('www.jiejieyh.cn','80','POST','/login.php'); $requestHead = [ 'Host'=>'www.jiejieyh.cn', 'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0', 'Accept'=>'application/json, text/javascript, */*; q=0.01', 'Accept-Language'=>'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded;', 'charset'=>'UTF-8', 'X-Requested-With'=>'XMLHttpRequest', 'Referer'=>'http://www.jiejieyh.cn', 'Content-Length'=>47, 'Connection'=>'keep-alive' ]; $requestContent = [ 'password' => 'admin', 'username' => '123456', ]; $result = $Http->setRequestHead($requestHead)->setRequestContent($requestContent)->sendRequest(); echo $result; ?>