C 实现HTTP GET请求MD5 认证

本文介绍了如何使用C语言编写HTTP GET请求,并结合CGI实现MD5身份验证。文章提供了Windows下的编译方法及源码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最新由于某个项目,需要实现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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值