gpt4 book ai didi

c - 通过c中的libcurl使用POST请求将文件上传到http(s)服务器

转载 作者:行者123 更新时间:2023-11-30 17:00:37 29 4
gpt4 key购买 nike

我想通过 post 请求将文件上传到 http 服务器,为此我使用 libcurl。

static int copy_to_http_server(char *src, char *disp_src, char srcflag,
char *dst, char *disp_dst, char dstflag)
{
CURL *curl;
CURLcode res;
char *url = disp_dst;
char *outfilename = src;

struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";

/* src = /users/play/team.jpg
* url = http://10.1.2.3/repo/team_server.jpg
* i want 'src' to be copied under 'repo' directory on http server and
* the file name on the server should be team_server.jpg or team.jpg is
* also fine */

curl_global_init(CURL_GLOBAL_ALL);

/* Fill in the file upload field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, src,
CURLFORM_END);

/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, src,
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);

curl = curl_easy_init();
/* initialize custom header list (stating that Expect: 100-continue is not
wanted */
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, url);
if (res != CURLE_OK) {
printf("could not set url : %s\n",curl_easy_strerror(res));
} else {
printf("able to set\n");
}
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

/* Perform the request, res will get the return code */
printf("calling curl_easy_perform\n");
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
printf("after calling curl_easy_perform\n");
/* always cleanup */
curl_easy_cleanup(curl);

/* then cleanup the formpost chain */
curl_formfree(formpost);
}
curl_global_cleanup();
return 1;
}

下面是我作为帖子响应得到的详细输出:-

> POST /repo/proc.out HTTP/1.1
Host: 10.1.2.3
Accept: */*
Content-Length: 0
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------8708d4558a12

< HTTP/1.1 404 Not Found
< Date: Sat, 28 May 2016 07:37:10 GMT
* Server Apache/2.2.15 (CentOS) is not blacklisted
< Server: Apache/2.2.15 (CentOS)
< Content-Length: 289
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /repo/proc.out was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 10.1.2.3 Port 80</address>
</body></html>
* Closing connection 0

最令人惊奇的部分是,即使使用命令行,我也无法上传文件,并且出现相同的错误:- curl -F filedata=@myfile.txt http://10.1.2.3/repo

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /repo/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 10.1.2.3 Port 80</address>
</body></html>

但我可以从同一路径下载文件,即“http://10.1.2.3/repo/available_file.jpeg

我需要在http服务器上配置任何额外的东西吗?或者应该在服务器上运行任何脚本/程序来将文件上传到服务器?

有人可以帮忙吗?

最佳答案

您需要一个 CGI 脚本来处理/repo 或/repo/proc.out 中的数据

CGI 脚本应使用您的 Web 服务器环境支持的语言编写。

该脚本将读取环境以及发送到脚本的数据,对其进行处理,然后将响应发送到您的curl 脚本。

某些 Web 服务器附带默认 CGI 脚本,该脚本只会将数据转储给您。它可能可以在 cgi-bin 目录中找到。

关于c - 通过c中的libcurl使用POST请求将文件上传到http(s)服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37498455/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com