> I am trying to find a way to upload a file to the server using
> WinHttp. I wrote a program to do this by using HTTP Post
> but I realized that my files contain special characters so Web
> server does not take what I sent entirely.
Are you posting the file as text or as binary? Sounds like you are doing the former when you should be doing the latter instead. Otherwise, you will have to decode the files and reencoding them in a way that the server will accept.
> For instance, my file contains & and = and thus, web server tries
> to parse my file instead of simply saving it. Is there any way I can
> tell the webserver not to parse files when I upload?
Sounds like you are not doing a binary upload to begin with. Please show your actual code.
--
Remy Lebeau (TeamB)
Followings are the part of my code.
PVOID pvFile = NULL;
DWORD dwFileSize;
WCHAR *filepath = L"abc.xml";
const WCHAR *http_types[] = {L"Content-Type: application/octet-stream
\r\n", NULL};
hFile = CreateFileW(filepath, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
dwFileSize = GetFileSize(hFile, NULL);
hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0,
dwFileSize, NULL);
if (hFileMap == NULL) {
printf("CreateFileMapping failed");
CloseHandle(hFile);
return FALSE;
}
pvFile = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
if (pvFile == NULL) {
printf("MapViewOfFile failed");
CloseHandle(hFileMap);
CloseHandle(hFile);
return FALSE;
}
// initiate SSL
hRequest = WinHttpOpenRequest(hConnect, L"POST", filename, /*L"/sd.py/
save_data"*/
NULL, WINHTTP_NO_REFERER, http_types, WINHTTP_FLAG_SECURE);
if (!hRequest) {
printf("WinHttpOpenRequest failed (%d)\n", GetLastError());
goto out;
}
// make self-signed certificate work
WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwOptions,
sizeof(DWORD));
if (WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, 0,
0, dwFileSize, 0) != TRUE) {
printf("WinHttpSendRequest failed (%d)\n", GetLastError());
goto out;
}
if (WinHttpWriteData(hRequest, pvFile, dwFileSize, &dwBytesWritten)
== FALSE) {
printf("WinHttpWriteData failed with %d\n", GetLastError());
}
if (!WinHttpReceiveResponse(hRequest, 0)) {
wprintf(L"WinHttpReceiveResponse failed (%d)\n", GetLastError());
goto out;
}
dwSize = sizeof(dwStatus);
if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE|
WINHTTP_QUERY_FLAG_NUMBER,
NULL, &dwStatus, &dwSize, NULL) == TRUE) {
printf("Returned status code: %d\n", dwStatus);
} else {
printf("WinHttpQueryHeader failed with %d\n", GetLastError());
}
out:
if (hRequest)
WinHttpCloseHandle(hRequest);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hOpen)
WinHttpCloseHandle(hOpen);
if (hFileMap)
CloseHandle(hFileMap);
if (hFile)
CloseHandle(hFile);
Please advise me what I am missing. Thank you.
YEH
> hRequest = WinHttpOpenRequest(..., http_types, ...);
You are telling the server that you will only accept "application/octet-stream" replies, but you are not telling the server that you are sending XML data to it. You need to use WinHttpAddRequestHeader(), or the pwszHeaders parameter of WinHttpSendRequest(), to specify a "Content-Type: text/xml; charset=..." header for the XML data, and make sure you fill in the actual charset that the XML is really using, or else the server will likely still not interpret the data correctly.
--
Remy Lebeau (TeamB)