小编bon*_*nii的帖子

在 C++ 上使用套接字进行 http 请求

我正在尝试在 Linux 上使用套接字执行 HTTP 请求,我的函数现在可以工作,但只能使用简单的域 aswww.google.comwebpage.000webhostapp.com。当我尝试添加路径和查询时webpage.000webhostapp.com/folder/file.php?parameter=value,它开始失败。

这是我的代码:

#include <iostream>
#include <cstring>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

int main(){
 int socket_desc;
 struct sockaddr_in serv_addr;
 struct hostent *server;
 char buffer[4096];

 std::string url = "www.exampleweb.com/folder/file.php";

 socket_desc = socket(AF_INET, SOCK_STREAM, 0);
 if(socket_desc < 0){
  std::cout<<"failed to create socket"<<std::endl;
 }

 server = gethostbyname(url.c_str());
 if(server==NULL){std::cout<<"could Not resolve hostname :("<<std::endl;}
 bzero((char *) &serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_port = htons(80);
 bcopy((char *)server->h_addr, …
Run Code Online (Sandbox Code Playgroud)

c++ sockets http request

3
推荐指数
1
解决办法
4512
查看次数

标签 统计

c++ ×1

http ×1

request ×1

sockets ×1