目的: 获取 HTTP头文件中的主机名
GET http://www.google.com.hk/ HTTP/1.1
Host: www.google.com.hk
Proxy-Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
#include <iostream>
#include <string>
int main(){
std::string str = "GET http://www.google.com.hk/ HTTP/1.1\r\n Host: www.google.com.hk\r\n Proxy-Connection: keep-alive\r\n User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 \r\n Safari/533.4\r\n Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n Accept-Encoding: gzip,deflate,sdch\r\n Accept-Language: en-US,en;q=0.8\r\n Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n";
std::size_t hpos = str.find("Host:");
std::size_t epos = str.find("\r\n", hpos);
std::string str2 = str.substr(hpos+5, epos-(hpos+5));
std::cout << str2 << std::endl;
return 0;
}