最新由于某个项目,需要实现http client cgi请求,就尝试用c写出来,顺便记录下。。。第一个博客。。。
修改了下windows下编译。
gcc -o http_send_cgi_msg http_send_cgi_msg.c -lcrypto
以下是源码:
http_send_cgi_msg.c
/*File : http_send_cgi_msg.c
*Auth : Leibo
*Date : 20171018
*Mail : [email protected]
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/md5.h>
#ifdef WIN32
#include <winsock2.h>
//#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#else
#include<unistd.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#define HTTP_METHOD_GET 1
#define HTTP_METHOD_POST 2
#define HTTP_MD5_LEN 16
#define HTTP_MD5_STR_LEN 33 //32+1
#define HTTP_SERVER_IP "10.10.10.114"
#define HTTP_SERVER_PORT 80
#define HTTP_STRING_SIZE 256
#define HTTP_BUFFER_SIZE 10240
#define HTTP_STATUS_OK 200
#define HTTP_STATUS_UNAUTHORIZED 401
#define HTTP_STATUS_ERROR 0
#define HTTP_AUTH_NC "00000001"
#define HTTP_AUTH_CNONCE "8c28955e90665322"
static char http_user[HTTP_STRING_SIZE] = "admin";
static char http_password[HTTP_STRING_SIZE] = "admin";
typedef struct _HTTP_MSG_PARA
{
int method;
char uri[HTTP_STRING_SIZE];
char user[HTTP_STRING_SIZE];
char pwd[HTTP_STRING_SIZE];
char body[HTTP_BUFFER_SIZE];
}HTTP_MSG_PARA;
typedef struct _HTTP_AUTH_PARA
{
char realm[HTTP_STRING_SIZE];
char qop[HTTP_STRING_SIZE];
char nonce[HTTP_STRING_SIZE];
char opaque[HTTP_STRING_SIZE];
}HTTP_AUTH_PARA;
#define HTTP_GET_URL "GET %s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n\r\n"
#define HTTP_GET_AUTH_URL "GET %s HTTP/1.1\r\nHost: %s:%d\r\nAccept: */*\r\n"\
"Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", opaque=\"%s\", qop=%s, nc=%s, cnonce=\"%s\"\r\n\r\n"
static int http_socket_create(const char *host, int port)
{
s