📄 http_main.cpp
字号:
#include "CSocket.h"#include <fstream>int main(){ string word; string filename; string hostname; int pos1 = 0; int pos2 = 0; cout << "enter the host name" << endl; cin >> word; //bulid the query for http string quest = "GET "; quest += word; quest += " HTTP/1.0\r\n"; quest += "User-agent:Mozilla/4.0\r\n"; quest += "Accept-language:zh-cn\r\n\r\n"; //get the hostname and filename from the word string str_http = "http://"; pos1 = word.find_first_of (str_http, 0); pos2 = word.find_first_of ("/", pos1+7); hostname = word.substr( pos1+7, pos2-pos1-7 ); pos1 = word.find_last_of( "/", word.size() ); filename = word.substr( pos1+1, word.size()-pos1-1 ); cout << "filename: " << filename << endl; cout << "hostname: " << hostname << endl; //use the hostname and port 80 to connect Sock_instance s_client( hostname, 80, CLIENT_TYPE ); if( !s_client.Connect() ) { cout << "connect error" << endl; return -1; } //send the http query to the host cout << "connect is ok!" << endl; if( !s_client.Send(quest) ) { cout << "send is error!" << endl; return -1; } //recieve all the file from the hsot while( s_client.Receive() > 0 ) { } //write the binary data which is recieved on the file FILE *fp = fopen( filename.c_str(), "wb" ); fwrite( s_client.data(), sizeof( unsigned char ), s_client.datalen(), fp ); fclose( fp ); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -