Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string sendNoCurl(const std::string& host, int port, const std::string& path,bool debug=false) {
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == -1) {
- std::cerr << "Error: Failed to create socket" << std::endl;
- return "";
- }
- struct sockaddr_in server;
- server.sin_family = AF_INET;
- server.sin_port = htons(port);
- if (inet_pton(AF_INET, host.c_str(), &server.sin_addr) <= 0) {
- std::cerr << "Error: Invalid address" << std::endl;
- close(sock);
- return "";
- }
- if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
- std::cerr << "Error: Connection failed" << std::endl;
- close(sock);
- return "";
- }
- std::string request = "get " + path + " http/1.0\r\n";
- //std::string request = "GET " + path + " HTTP/1.1\r\n";
- request += "Host: " + host + "\r\n";
- request += "Connection: close\r\n\r\n";
- cout <<"DEBUG NO CURL :"<<request<<endl;
- if (send(sock, request.c_str(), request.length(), 0) != request.length()) {
- std::cerr << "Error: Failed to send request" << std::endl;
- close(sock);
- return "";
- }
- std::string response;
- char buffer[4096];
- int bytes_received;
- while ((bytes_received = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
- response.append(buffer, bytes_received);
- }
- close(sock);
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement