c++ linux简单http 客户端

62次阅读
没有评论

共计 1179 个字符,预计需要花费 3 分钟才能阅读完成。

#include <sys/types.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fstream>
#include <iostream> 
using namespace std;
int main(){
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char *strings="GET / HTTP/1.1\r\nHost: 14.215.177.38\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nAccept-Language: zh-CN,zh;q=0.9\r\nConnection: Close\r\n\r\n";
    char ch;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("14.215.177.38");
    address.sin_port = htons(80);
    len = sizeof(address);
    result = connect(sockfd,  (struct sockaddr *)&address, len);
    if(result == -1){
        perror("oops: client1");
        return 1;
    }
    write(sockfd,strings,strlen(strings));
    ofstream outfile ( "baidu.txt" );
    if(outfile.fail()){
        cout << "打开错误" << endl;
        exit(0);
    }
    while(read(sockfd,&ch,1))
        // printf("%c", ch);
        outfile << ch ;
    close(sockfd);
    outfile.close();
    return 1;
}

 

正文完
 
admin
版权声明:本站原创文章,由 admin 2022-02-10发表,共计1179字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码