Chinaunix
标题:
求助,HTML POST 用C语言实现。
[打印本页]
作者:
沉水之鱼
时间:
2017-08-23 16:41
标题:
求助,HTML POST 用C语言实现。
我现在在测试一个服务器的POST文件,就是上传一个图片,我写的HTML 测试是可以上传成功的。
<!DOCTYPE html>
<html>
<head>
<title>this is post test</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action=" http://testing-only.covert-wireless.com/v0/photos" enctype="multipart/form-data">
<input type="text" name="cameraCode" value="014286000210947">
<input type="text" name="latitude" value="38.1234312">
<input type="text" name="longitude" value="54.4123412">
<input type="text" name="temperature" value="52.2">
<input type="text" name="date" value="08/23/2017 ">
<input type="text" name="time" value="20:22:01">
<input type="text" name="signal" value="4">
<input type="text" name="battery" value="2">
<input type="text" name="capacity" value="3788">
<input type="text" name="freeSpace" value="2344">
<input type="text" name="fileId" value="650001">
<input type="text" name="firmware" value="V4.0.0.1">
<input type="file" name="file"/>
<input type="submit">
</form>
</body>
</html>
复制代码
下面是我用C语言写的,但是一直调不通,求大神指教:
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
int fd;
unsigned long get_file_size(const char *path)
{
unsigned long filesize = -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0){
return filesize;
}else{
filesize = statbuff.st_size;
}
return filesize;
}
void httpPost(void)
{
int ret = 0;
FILE* fp = NULL;
char *pHeader = "POST /v0/photos HTTP/1.1\r\n"
"Host: testing-only.covert-wireless.com\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length:";
char *pBodyInfo = "cameraCode:014286000210947\r\n"
"latitude:38.1234312\r\n"
"longitude:54.4123412\r\n"
"temperature:52.2\r\n"
"date:06/12/2017\r\n"
"time:20:22:01\r\n"
"signal:4\r\n"
"battery:2\r\n"
"capacity:3788\r\n"
"freeSpace:2344\r\n"
"fileId:650001\r\n"
"firmware:V4.0.0.1\r\n"
"file:";
char* tail = "\r\n\r\n";
char pHeaderStr[4096] = {0};
int filesize = get_file_size("./t.jpg");
printf("filesize = %d\n", filesize);
int contentLen = strlen(pBodyInfo) + filesize ;//+ strlen(tail);
char pLenstr[32] = {0};
sprintf(pLenstr, "%d", contentLen);
strcat(pHeaderStr, pHeader);
strcat(pHeaderStr, pLenstr);
strcat(pHeaderStr, "\r\n\r\n");
printf("%s", pHeaderStr);
ret = send(fd, pHeaderStr, strlen(pHeaderStr), 0);
printf("~~~~~~~~~~~ret = %d\n", ret);
ret = send(fd, pBodyInfo, strlen(pBodyInfo), 0);
printf("~~~~~~~~~~~ret = %d\n", ret);
char fileData[20*1024] = {0};
fp = fopen("./t.jpg", "r");
if(NULL == fp)
{
printf("photo open failed!\n");
}
fseek(fp, 0, SEEK_SET);
ret = fread(fileData, 1, filesize, fp);
if(ret != filesize)
{
printf("read file error!\n");
}
ret = send(fd, fileData, filesize, 0);
printf("~~~~~~~~~~~ret = %d\n", ret);
//ret = send(fd, tail, strlen(tail), 0);
//printf("~~~~~~~~~~~ret = %d\n", ret);
}
void *ReadThread(void *args)
{
char buffer[4096] = {0};
int len = 0;
while(1)
{
memset(buffer, 0, 4096);
len = recv(fd, buffer, 4096, 0);
if(len < 0)
{
printf("recv error!\n");
}
else if(len > 0)
{
printf("%s\n", buffer);
}
}
}
int main()
{
int ret;
struct sockaddr_in servaddr = {0};
pthread_t threadId;
fd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("52.5.65.32"); //inet_addr("52.71.137.108");34.199.91.254
servaddr.sin_port = htons(80);
if(0 != connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr)))
{
printf("connect server failed!\n");
return -1;
}
else
{
printf("connect server success!\n");
}
ret = pthread_create(&threadId, NULL, ReadThread, NULL);
if(0 != ret)
{
printf("ReadThread create failed!\n");
}
else
{
printf("ReadThread create success!\n");
}
httpPost();
while(1)
{
usleep(100000000);
}
return 0;
}
复制代码
作者:
sxcong
时间:
2017-08-24 15:26
用libcurl吧,10几行代码,很简单。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2