使用http post在C语言中编写套接字

时间:2013-04-22 09:40:21

标签: c sockets http-post

想在windows7中使用c进行客户端 - 服务器编程,它应该使用http POST方法将字符串发送到服务器。 POST方法中的参数应包括ip-address等:

我从 http://souptonuts.sourceforge.net/code/http_post.c.html 获取此代码并将其更改为在Windows上运行它,但仍然会出现1错误:

#ifdef WIN32
  #include <winsock2.h>
  #include <ws2tcpip.h>
  #include <windows.h>
#else
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
  #include <sys/wait.h>
  #include <netdb.h>
  #include <assert.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>

#define SA      struct sockaddr
#define MAXLINE 4096
#define MAXSUB  200


#define LISTENQ 1024

extern int h_errno;

ssize_t process_http(int sockfd, char *host, char *page, char *poststr)
{
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    ssize_t n;
    snprintf(sendline, MAXSUB,
         "POST %s HTTP/1.0\r\n"
         "Host: %s\r\n"
         "Content-type: application/x-www-form-urlencoded\r\n"
         "Content-length: %d\r\n\r\n"
         "%s", page, host, strlen(poststr), poststr);

    write(sockfd, sendline, strlen(sendline));
    while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = '\0';
        printf("%s", recvline);
    }
    return n;

}
int main(void)
{
    int sockfd;
    struct sockaddr_in servaddr;

    char **pptr;
    //********** You can change. Puy any values here *******
    char *hname = "souptonuts.sourceforge.net";
    char *page = "/chirico/test.php";
    char *poststr = "mode=login&user=test&password=test\r\n";
    //*******************************************************

    char str[50];
    struct hostent *hptr;
    if ((hptr = gethostbyname(hname)) == NULL) {
        fprintf(stderr, " gethostbyname error for host: %s: %s",
            hname, hstrerror(h_errno));
        exit(1);
    }
    printf("hostname: %s\n", hptr->h_name);
    if (hptr->h_addrtype == AF_INET
        && (pptr = hptr->h_addr_list) != NULL) {
        printf("address: %s\n",
               inet_ntop(hptr->h_addrtype, *pptr, str,
                 sizeof(str)));
    } else {
        fprintf(stderr, "Error call inet_ntop \n");
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    // bzero(&servaddr, sizeof(servaddr));
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(80);
    inet_pton(AF_INET, str, &servaddr.sin_addr);

    connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
    process_http(sockfd, hname, page, poststr);
    close(sockfd);
    exit(0);

}

MinGW编译器出现的错误是:

httppost.c:33:12: error: conflicting types for 'WSAGetLastError'
In file included from httppost.c:5:0:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/winsock2.h:594:32: n
e: previous declaration of 'WSAGetLastError' was here

2 个答案:

答案 0 :(得分:2)

您所拥有的代码是基于Linux的系统,但遗憾的是,在MinGW(Windows)中,标识符h_errno之前已被采用。

问题在于这一行

extern int h_errno;

以前在windows头文件中定义,然后你不能使用它:

#define h_errno WSAGetLastError()

只需使用其他标识符代替h_errno,甚至只删除该行!

答案 1 :(得分:1)