#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include <unistd.h>
unsigned char *http_content_pack(unsigned char *filename, unsigned char *acid, unsigned char *fileType);
unsigned char *http_head_pack(unsigned char *addr, unsigned char *data_cont, unsigned int cont_len);
int http_conn(unsigned char *addr);
int http_send(int sockfd, unsigned char *buf, unsigned int nbytes);
//int http_send(int sockfd, unsigned long aulMsg[2], unsigned int nbytes);
unsigned char *http_content_pack(unsigned char *filename, unsigned char *acid, unsigned char *fileType)
{
const unsigned char *boundary_start = "--------------------------ec40f8276ca0f20f\r\n";
const unsigned char *boundary_end = "\r\n--------------------------ec40f8276ca0f20f\r\n";
const unsigned char *boundary_last = "\r\n--------------------------ec40f8276ca0f20f--\r\n";
unsigned char cont_desc[512];
sprintf(cont_desc, "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\nContent-Type: text/plain\r\n\r\n", filename);
int fd;
unsigned int file_size = 0;
unsigned int data_size = 0;
int res;
unsigned char *data_buf = NULL;
unsigned char *file_buf = NULL;
struct stat st_buf;
file_size = st_buf.st_size;
data_size = file_size + 1024;
data_buf = malloc(data_size);
if(data_buf == NULL)
{
return data_buf;
}
if(stat(filename, &st_buf) < 0)
{
return data_buf;
}
file_buf = malloc(file_size);
if(file_buf == NULL)
{
return data_buf;
}
fd = open(filename, O_RDONLY, 0);
if(fd < 0)
{
free(file_buf);
return data_buf;
}
res = read(fd, file_buf, file_size);
if(res < 0)
{
free(file_buf);
return data_buf;
}
strcat(data_buf, boundary_start);
strncat(data_buf, cont_desc, strlen(cont_desc));
strcat(data_buf, file_buf);
strcat(data_buf, boundary_end);
strcat(data_buf, "Content-Disposition: form-data; name=\"acid\"\r\n\r\n");
strcat(data_buf, acid);
strcat(data_buf, boundary_end);
strcat(data_buf, "Content-Disposition: form-data; name=\"type\"\r\n\r\n");
strcat(data_buf, fileType);
strcat(data_buf, boundary_last);
free(file_buf);
return data_buf;
}
unsigned char *http_head_pack(unsigned char *ip_str, unsigned char *data_cont, unsigned int cont_len)
{
unsigned char req[512] = {0};
unsigned int all_size = 0;
unsigned char *all_buf = NULL;
unsigned char *boundary = "------------------------ec40f8276ca0f20f";
sprintf(req, "POST /acds/api/upload HTTP/1.1\r\n"
"User-Agent: curl/7.35.0\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
"Content-Length: %d\r\n"
"Expect: 100-continue\r\n"
"Content-Type:multipart/form-data; boundary=%s\r\n\r\n",
ip_str, cont_len, boundary);
all_size = strlen(req) + cont_len;
all_buf = malloc(all_size);
if(all_buf == NULL)
{
return all_buf;
}
strcat(all_buf, req);
strcat(all_buf, data_cont);
return all_buf;
}
int http_conn(unsigned char *ip_str)
{
int sockfd;
int res;
struct sockaddr_in dst;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
return -1;
}
bzero(&dst, sizeof(dst));
dst.sin_family = AF_INET;
dst.sin_port = htons(8080);
dst.sin_addr.s_addr = inet_addr(ip_str);
res = connect(sockfd, (struct sockaddr *)&dst, sizeof(dst));
if(res < 0)
{
close(sockfd);
return -1;
}
return sockfd;
}
int http_send(int sockfd, unsigned char *buf, unsigned int nbytes)
{
unsigned int send_nbytes = 0;
unsigned int res;
while(send_nbytes < nbytes)
{
res = write(sockfd, buf + send_nbytes, nbytes - send_nbytes);
if(res == -1)
{
return res;
}
send_nbytes += res;
}
return 1;
}
unsigned int CPM_PostFile(unsigned char *postAddr, unsigned char *postFileName, unsigned char *acid, unsigned char *fileType)
{
int fd;
unsigned char *pdata = NULL;
unsigned char *phead = NULL;
unsigned long aulMsg[2];
pdata = http_content_pack(postFileName, acid, fileType);
if (pdata == NULL)
{
return 1;
}
phead = http_head_pack(postAddr, pdata, strlen(pdata));
if (phead == NULL)
{
free(pdata);
return 1;
}
fd = http_conn(postAddr);
if (fd < 0)
{
free(pdata);
free(phead);
return 1;
}
http_send(fd, phead, strlen(phead);
free(pdata);
free(phead);
close(fd);
return 1;
}
int main()
{
CPM_PostFile("092.168.1.100", "test_file.log", "test_id", "log");
}
http_test.rar_Linux C http_POST 文件 HTTP_The Test_c http_c语言 h
版权申诉
115 浏览量
2022-09-14
22:37:40
上传
评论
收藏 1KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- c语言对对碰游戏源码.zip
- 新录音.m4a
- shufflenet模型-基于人工智能的卷积网络训练识别中药饮片-不含数据集图片-含逐行注释和说明文档.zip
- resnet模型-基于图像分类算法对7种水果识别-不含数据集图片-含逐行注释和说明文档.zip
- JMeter使用教程.md
- JMeter使用教程.md
- mobilenet模型-基于图像分类算法对农作物病害部位识别-不含数据集图片-含逐行注释和说明文档.zip
- 【资源免费分享】MP4文件自动分轨工具(内附使用录像介绍)
- densenet模型-基于人工智能的卷积网络训练识别人行为-不含数据集图片-含逐行注释和说明文档.zip
- JMeter使用教程.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈