利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下:
HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端。
代码截图:
特别注意每一行末的\r\n
下面是代码实现:
//mySocket.h头文件
#include <Winsock2.h> #include <Windows.h> #include <Ws2tcpip.h> #pragma comment(lib, "ws2_32.lib") #include <string> using namespace std; //只是对Socket中使用TCP协议的一种封装 class MySocket{ public: static int InitClient(SOCKET *sock, string ip, int port); static int CloseMySocket(SOCKET *Sock); static int SendData(SOCKET sock, const string data); static int RecvData(SOCKET sock, string &data); };