利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下:

socket编程实现HTTP请求

 

HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端。

socket编程实现HTTP请求

 

代码截图:

特别注意每一行末的\r\n

 

socket编程实现HTTP请求

下面是代码实现:

 

//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);
};
View Code

相关文章:

  • 2021-06-28
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-07-20
  • 2021-10-14
  • 2022-03-06
相关资源
相似解决方案