Hallo Leute,
schon seit längerem versuche ich, größere Textdateien mithilfe von WinHttp an ein php skript zu übergeben und hochzuladen.
Allerdings gibt es immer irgendetwas das nicht funktioniert, wobei Dateien oder Strings, die kleiner als 250 Byte sind, problemlos mit Winsock hochgeladen werden.
Ich habe daher versucht, mit Winsock eine größere Datei hochzuladen, was allerdings nicht funktionierte.
Später, bin ich dann auf WinHttp gestoßen und versuche seitdem, mit diesem eine große Datei erfolgreich hochzuladen, wobei ich aber immer an der korrekten Gestaltung des Request headers scheitere.
Zwar habe ich mit Wireshark mitgesnifft, wie ein solches Request aussehen muss, aber bei der Umsetzung hapert es doch gewaltig.
Deshalb meine Frage: Hat hier jemand schon einmal ähnliches versucht oder eine Lösung dafür gefunden?
Hier der PHP Code:
[cli]<?php
if(!is_dir("uploads")) mkdir("uploads");
if(!isset($post)) {
echo "<form action=upload.php method=post enctype=multipart/form-data>
<input type=file name=upfile><br>
<input type=submit name=post value=hochladen>";
}
else {
if($upfile !== "") { copy($upfile,"uploads/".$upfile_name); echo "Datei hochgeladen ! <br> <a href='uploads/$upfile_name'>$upfile_name</a>";}
else { echo "Keine Datei ausgewählt<br><a href=javascript:history.back()>zurück</a>";}
}
?>[/cli]
und hier mein C Code:
[cli]#include <stdio.h>
#include <windows.h>
#include "winhttp.h"
#include <string.h>
int main()
{
HINTERNET hopen, hconnect, hhttp;
BOOL r, retsend, bResults;
DWORD read;
char header[1024], head[512], head1[512];
char *to_send="hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo weltv"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welt"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welt"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo";
char *speichername = "test32.txt";
memset(head, 0, 10);
memset(header, 0, 10);
sprintf(head, "%s-----------------------------90721038027008\r\n", head);
sprintf(head, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", head, speichername);
sprintf(head, "%sContent-Type: text/plain\r\n", head);
char *zeiger = head;
printf("%s", zeiger);
//sprintf(header, "%s-----------------------------90721038027008\r\n", header); //ein '-' weniger
//sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, speichername);
//sprintf(header, "%sContent-Type: text/plain\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s%s\r\n", header, to_send);
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"post\"\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%shochladen\r\n\r\n", header);
sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n", header);
hopen = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if(hopen == NULL)
printf("Fehler InternetOpen()");
hconnect = WinHttpConnect(hopen, L"www.beispiel.de",
INTERNET_DEFAULT_HTTP_PORT,0);
if(hconnect == NULL)
printf("Fehler InternetConnect()");
hhttp = WinHttpOpenRequest(hconnect, L"POST", L"/upload.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,0);
if(hhttp == NULL)
printf("Fehler HttpOpenRequest()");
bResults = WinHttpAddRequestHeaders( hhttp,
L"Content-Type: multipart/form-data; boundary=---------------------------90721038027008",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD );
if(!bResults)
printf("Fehler WinHttpAddRequestHeaders()\n");
read=GetLastError();
switch(read)
{
case ERROR_WINHTTP_INCORRECT_HANDLE_STATE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_STATE");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_TYPE");
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
printf("ERROR_WINHTTP_INTERNAL_ERROR");
break;
case ERROR_NOT_ENOUGH_MEMORY:
printf("ERROR_NOT_ENOUGH_MEMORY");
break;
case ERROR_INVALID_PARAMETER:
printf("ERROR_INVALID_PARAMETER");
break;
default:
printf("NO ERROR DETECTED\n");
break;
}
bResults = WinHttpSendRequest( hhttp,
(LPVOID)(LPCSTR)zeiger,
(DWORD)strlen(head),
(LPVOID)(LPCSTR)header,
strlen(header),
strlen(header),
0 );
if(!bResults)
printf("Fehler WinHttpSendRequest()\n");
DWORD err;
err = GetLastError();
switch(err)
{
case ERROR_WINHTTP_CANNOT_CONNECT:
printf("ERROR_WINHTTP_CANNOT_CONNECT");
break;
case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED:
printf("ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED");
break;
case ERROR_WINHTTP_CONNECTION_ERROR:
printf("ERROR_WINHTTP_CONNECTION_ERROR");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_STATE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_STATE");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_TYPE");
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
printf("ERROR_WINHTTP_INTERNAL_ERROR");
break;
case ERROR_WINHTTP_INVALID_URL:
printf("ERROR_WINHTTP_INVALID_URL");
break;
case ERROR_WINHTTP_LOGIN_FAILURE:
printf("ERROR_WINHTTP_LOGIN_FAILURE");
break;
case ERROR_WINHTTP_NAME_NOT_RESOLVED:
printf("ERROR_WINHTTP_NAME_NOT_RESOLVED");
break;
case ERROR_WINHTTP_OPERATION_CANCELLED:
printf("ERROR_WINHTTP_OPERATION_CANCELLED");
break;
case ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW:
printf("ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW");
break;
case ERROR_WINHTTP_SECURE_FAILURE:
printf("ERROR_WINHTTP_SECURE_FAILURE");
break;
case ERROR_WINHTTP_SHUTDOWN:
printf("ERROR_WINHTTP_SHUTDOWN");
break;
case ERROR_WINHTTP_TIMEOUT:
printf("ERROR_WINHTTP_TIMEOUT");
break;
case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
printf("ERROR_WINHTTP_UNRECOGNIZED_SCHEME");
break;
case ERROR_NOT_ENOUGH_MEMORY:
printf("ERROR_NOT_ENOUGH_MEMORY");
break;
case ERROR_INVALID_PARAMETER:
printf("ERROR_INVALID_PARAMETER");
break;
case ERROR_WINHTTP_RESEND_REQUEST:
printf("ERROR_WINHTTP_RESEND_REQUEST");
break;
}
WinHttpCloseHandle(hopen);
WinHttpCloseHandle(hconnect);
WinHttpCloseHandle(hhttp);
return EXIT_SUCCESS;
}
[/cli]
Mfg
Faustpatrone
schon seit längerem versuche ich, größere Textdateien mithilfe von WinHttp an ein php skript zu übergeben und hochzuladen.
Allerdings gibt es immer irgendetwas das nicht funktioniert, wobei Dateien oder Strings, die kleiner als 250 Byte sind, problemlos mit Winsock hochgeladen werden.
Ich habe daher versucht, mit Winsock eine größere Datei hochzuladen, was allerdings nicht funktionierte.
Später, bin ich dann auf WinHttp gestoßen und versuche seitdem, mit diesem eine große Datei erfolgreich hochzuladen, wobei ich aber immer an der korrekten Gestaltung des Request headers scheitere.
Zwar habe ich mit Wireshark mitgesnifft, wie ein solches Request aussehen muss, aber bei der Umsetzung hapert es doch gewaltig.
Deshalb meine Frage: Hat hier jemand schon einmal ähnliches versucht oder eine Lösung dafür gefunden?
Hier der PHP Code:
[cli]<?php
if(!is_dir("uploads")) mkdir("uploads");
if(!isset($post)) {
echo "<form action=upload.php method=post enctype=multipart/form-data>
<input type=file name=upfile><br>
<input type=submit name=post value=hochladen>";
}
else {
if($upfile !== "") { copy($upfile,"uploads/".$upfile_name); echo "Datei hochgeladen ! <br> <a href='uploads/$upfile_name'>$upfile_name</a>";}
else { echo "Keine Datei ausgewählt<br><a href=javascript:history.back()>zurück</a>";}
}
?>[/cli]
und hier mein C Code:
[cli]#include <stdio.h>
#include <windows.h>
#include "winhttp.h"
#include <string.h>
int main()
{
HINTERNET hopen, hconnect, hhttp;
BOOL r, retsend, bResults;
DWORD read;
char header[1024], head[512], head1[512];
char *to_send="hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo weltv"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welt"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welt"
"hallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo welthallo";
char *speichername = "test32.txt";
memset(head, 0, 10);
memset(header, 0, 10);
sprintf(head, "%s-----------------------------90721038027008\r\n", head);
sprintf(head, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", head, speichername);
sprintf(head, "%sContent-Type: text/plain\r\n", head);
char *zeiger = head;
printf("%s", zeiger);
//sprintf(header, "%s-----------------------------90721038027008\r\n", header); //ein '-' weniger
//sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, speichername);
//sprintf(header, "%sContent-Type: text/plain\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s%s\r\n", header, to_send);
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"post\"\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%shochladen\r\n\r\n", header);
sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n", header);
hopen = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if(hopen == NULL)
printf("Fehler InternetOpen()");
hconnect = WinHttpConnect(hopen, L"www.beispiel.de",
INTERNET_DEFAULT_HTTP_PORT,0);
if(hconnect == NULL)
printf("Fehler InternetConnect()");
hhttp = WinHttpOpenRequest(hconnect, L"POST", L"/upload.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,0);
if(hhttp == NULL)
printf("Fehler HttpOpenRequest()");
bResults = WinHttpAddRequestHeaders( hhttp,
L"Content-Type: multipart/form-data; boundary=---------------------------90721038027008",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD );
if(!bResults)
printf("Fehler WinHttpAddRequestHeaders()\n");
read=GetLastError();
switch(read)
{
case ERROR_WINHTTP_INCORRECT_HANDLE_STATE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_STATE");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_TYPE");
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
printf("ERROR_WINHTTP_INTERNAL_ERROR");
break;
case ERROR_NOT_ENOUGH_MEMORY:
printf("ERROR_NOT_ENOUGH_MEMORY");
break;
case ERROR_INVALID_PARAMETER:
printf("ERROR_INVALID_PARAMETER");
break;
default:
printf("NO ERROR DETECTED\n");
break;
}
bResults = WinHttpSendRequest( hhttp,
(LPVOID)(LPCSTR)zeiger,
(DWORD)strlen(head),
(LPVOID)(LPCSTR)header,
strlen(header),
strlen(header),
0 );
if(!bResults)
printf("Fehler WinHttpSendRequest()\n");
DWORD err;
err = GetLastError();
switch(err)
{
case ERROR_WINHTTP_CANNOT_CONNECT:
printf("ERROR_WINHTTP_CANNOT_CONNECT");
break;
case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED:
printf("ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED");
break;
case ERROR_WINHTTP_CONNECTION_ERROR:
printf("ERROR_WINHTTP_CONNECTION_ERROR");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_STATE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_STATE");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_TYPE");
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
printf("ERROR_WINHTTP_INTERNAL_ERROR");
break;
case ERROR_WINHTTP_INVALID_URL:
printf("ERROR_WINHTTP_INVALID_URL");
break;
case ERROR_WINHTTP_LOGIN_FAILURE:
printf("ERROR_WINHTTP_LOGIN_FAILURE");
break;
case ERROR_WINHTTP_NAME_NOT_RESOLVED:
printf("ERROR_WINHTTP_NAME_NOT_RESOLVED");
break;
case ERROR_WINHTTP_OPERATION_CANCELLED:
printf("ERROR_WINHTTP_OPERATION_CANCELLED");
break;
case ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW:
printf("ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW");
break;
case ERROR_WINHTTP_SECURE_FAILURE:
printf("ERROR_WINHTTP_SECURE_FAILURE");
break;
case ERROR_WINHTTP_SHUTDOWN:
printf("ERROR_WINHTTP_SHUTDOWN");
break;
case ERROR_WINHTTP_TIMEOUT:
printf("ERROR_WINHTTP_TIMEOUT");
break;
case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
printf("ERROR_WINHTTP_UNRECOGNIZED_SCHEME");
break;
case ERROR_NOT_ENOUGH_MEMORY:
printf("ERROR_NOT_ENOUGH_MEMORY");
break;
case ERROR_INVALID_PARAMETER:
printf("ERROR_INVALID_PARAMETER");
break;
case ERROR_WINHTTP_RESEND_REQUEST:
printf("ERROR_WINHTTP_RESEND_REQUEST");
break;
}
WinHttpCloseHandle(hopen);
WinHttpCloseHandle(hconnect);
WinHttpCloseHandle(hhttp);
return EXIT_SUCCESS;
}
[/cli]
Mfg
Faustpatrone