Ich möchte eine Datei auf http-Server mit Post-Anfrage hochladen, dafür benutze ich libcurl.Hochladen einer Datei auf http (s) Server mit POST-Anfrage über libcurl in c
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;
}
unten ist die ausführliche Ausgabe, die ich als Post Antwort bin immer: -
> 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
Erstaunlichste ist, ich nicht in der Lage bin, die Datei auch mit Befehlszeile, und immer gleiche Fehler zu laden: - curl -F [email protected] 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>
, aber ich bin in der Lage, eine Datei aus dem gleichen Pfad zum Download dh "http://10.1.2.3/repo/available_file.jpeg"
brauche ich etwas extra zu konfigurieren auf http server oder irgendein script/programm sollte auf dem server laufen um eine datei auf den server zu laden?
könnte jemand bitte helfen?
Was läuft auf dem Server? Können Sie den Handler für die Postanforderung anzeigen? – cnicutar
ein 404 zurück bedeutet, dass Sie dies nicht einmal an die richtige URL senden ... –
@DanielStenberg Ich kann auf "http://10.1.2.3/repo/" auf meinem Webbrowser zugreifen und es listet alle Dateien und Verzeichnis verfügbar. und angenommen, es enthält "available_file.jpeg", dann kann ich es durch curl-anfrage herunterladen, indem ich die url als "http://10.1.2.3/repo/available_file.jpeg" übergebe. Jetzt möchte ich eine Datei hochladen "send_file.jpeg" an "http://10.1.2.3/repo/" mit POST aber es sagt "Die angeforderte URL/Repo/wurde nicht auf diesem Server gefunden". Wie kann dies bei POST nicht möglich sein? – r4nj4n