linux平台C C++实现http客户端模拟浏览器获取html

linux平台C/C++实现http客户端模拟浏览器获取html


DATE: 2017-12-21 15:59:18

Windows平台参考代码

代码Copy自Linux下用c语言实现发送http请求

本文将代码抄录至此,附调试数据

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> 
#define DEST_PORT 80
#define DEST_IP_ADDR "119.29.15.186"
#define DEST_IP_BY_NAME "www.yixzm.cn"
void process_info(int fd) {
    int send_num;
    char send_buf [] = "helloworld";
    char recv_buf [4096];
    char str1[4096];
    while (1) {
        printf("begin send\n");
        memset(str1,0,4096);
        strcat(str1, "POST http://www.yixzm.cn/tools/cppcgi.php?instr=xxcx HTTP/1.1\r\n");
        strcat(str1,"Host: www.yixzm.cn\r\n");
        strcat(str1,"Content-Length: 65\r\n");
        strcat(str1,"Content-Type: application/x-www-form-urlencoded\r\n");
        strcat(str1,"\r\n");
        strcat(str1,"mathod=adb_signe&token=0E1FEECD0EE54E3B8568A536A7036D78B1AC7EEE");
        strcat(str1,"\r\n\r\n");
        printf("str1 = %s\n",str1);
        send_num = send(fd, str1,strlen(str1),0);
        if (send_num < 0) {
            perror("send error");
            exit(1);
        } else {
            printf("send successful\n");
            printf("begin recv:\n");
            int recv_num = recv(fd,recv_buf,sizeof(recv_buf),0);
            if(recv_num < 0) {
                perror("recv");
                exit(1);
            } else {
                printf("recv sucess:%s\n",recv_buf);
            }
        }
        break;
        sleep(5);
    }
}


int main()
{
    int sock_fd;
    struct sockaddr_in addr_serv;
    sock_fd=socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd < 0)
    {
        perror("sock error");
        exit(1);
    }
    else
    {
        printf("sock successful");      
    }

    struct hostent* hostInfo = gethostbyname(DEST_IP_BY_NAME);
    if(NULL == hostInfo){
        printf("hostInfo is null\n");
        return -6;
    }

    memset(&addr_serv, 0, sizeof(addr_serv));
    addr_serv.sin_family = AF_INET;
    addr_serv.sin_port = htons(DEST_PORT);
    //addr_serv.sin_addr.s_addr = inet_addr(DEST_IP_ADDR);

     printf("Ip address = %s \n",inet_ntoa(*((struct in_addr*)hostInfo->h_addr)));
    memcpy(&addr_serv.sin_addr, &(*hostInfo->h_addr_list[0]), hostInfo->h_length);

    if (connect(sock_fd, (struct sockaddr*)(&addr_serv), sizeof(addr_serv)) < 0)
    {
        perror("connect error\n");
        exit(1);
    }
    else
    {
        printf("connect successful\n");
    }
    process_info(sock_fd);
}

服务端使用命令 ngrep port 80监听Apache 80端口的交互数据。
如果服务器没有ngrep,需要yum install ngrep

监听到的数据如下:

###########
T 180.168.174.128:49934 -> 10.135.42.160:80 [AP]
  POST http://www.yixzm.cn/tools/cppcgi.php?instr=xxcx HTTP/1.1..Host: www.yix
  zm.cn..Content-Length: 65..Content-Type: application/x-www-form-urlencoded..
  ..mathod=adb_signe&token=0E1FEECD0EE54E3B8568A536A7036D78B1AC7EEE....       
##
T 10.135.42.160:80 -> 180.168.174.128:49934 [AP]
  HTTP/1.1 200 OK..Date: Thu, 21 Dec 2017 07:37:15 GMT..Server: Apache/2.2.15 
  (CentOS)..X-Powered-By: PHP/5.3.3..Vary: Accept-Encoding..Content-Length: 83
  6..Connection: close..Content-Type: text/html; charset=UTF-8....<html>.<head
  >.        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'
   />.        <title> void * Future </title>..        <style type="text/css">.
          <!--.        @import url(style.css);.        -->.        </style>.</
  head>....<body>..<form name="formcmd" action="index.php?cmd">...<input type=
  "text" name="instr" id="cmd" />...<input type="button" name="button" value="
  ......" onclick="outputMan()" />..</form>..<iframe src="../../cgi-bin/bin/cp
  p.cgi?xxcx" id="iframe" name="iframe" frameborder="0" scrolling="no"></ifram
  e>..</body>.<footer>..&copy ............ ...ICP...17050606...-1&nbsp........
  ....31011502400367...</footer>..<script language="javascript">.function outp
  utMan(){..var cmd = formcmd.instr.value;..document.getElementById("iframe").
  src = "../../cgi-bin/bin/cpp.cgi?" + cmd; .}.</script>..</html>.            
########

其中,ccxc是从Client端传来的字段值。

版权声明

弈心博客


本文首发site_name,转载请附上博文链接!