سلام
من قصد ارسال فایل به یک صفحه aspx رو دارم
قطعه کد زیر رو دارم استفاده می کنم. اما در سمت مقصد، request بدون هیچگونه فایلی دریافت میشه(Request.Files.Count==0)
int main()
{
HINTERNET m_sessionHandle;
wstring m_requestHost;
m_sessionHandle = WinHttpOpen(L"my userAgent",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
wstring m_requestURL = L"http://localhost/index.aspx?a=1";
wchar_t szHostName[MAX_PATH] = L"";
wchar_t szURLPath[MAX_PATH * 5] = L"";
URL_COMPONENTS urlComp;
memset(&urlComp, 0, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
urlComp.lpszHostName = szHostName;
urlComp.dwHostNameLength = MAX_PATH;
urlComp.lpszUrlPath = szURLPath;
urlComp.dwUrlPathLength = MAX_PATH * 5;
urlComp.dwSchemeLength = 1; // None zero
if (WinHttpCrackUrl(m_requestURL.c_str(), m_requestURL.size(), 0, &urlComp))
{
m_requestHost = szHostName;
HINTERNET hConnect = NULL;
hConnect = WinHttpConnect(m_sessionHandle, szHostName, urlComp.nPort, 0);
DWORD dwOpenRequestFlag = (urlComp.nScheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0;
HINTERNET hRequest = NULL;
hRequest = WinHttpOpenRequest(
hConnect,
L"POST",
urlComp.lpszUrlPath,
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag);
if (hRequest != NULL)
{
wstring m_additionalRequestHeaders = L"Content-Type: multipart/form-data; boundary=-----------------------------myfile";
//WinHttpAddRequestHeaders(hRequest, m_additionalRequestHeaders.c_str(), m_additionalRequestHeaders.size(), WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON);
CHAR* pszTxt = "-----------------------------myfile\r\n"
"Content-Deposition: form-data;name="myfile"; filename="thfile.zip"\r\n"
"Content-Type: application/zip; charset=utf-8\r\n\r\n";
size_t fileLength;
byte* filebytes = (BYTE*)GetFileBytes("myfile.zip", fileLength);
DWORD totalLength = (lstrlenA(pszTxt) + fileLength + lstrlenA("\r\n=-----------------------------myfile--\r\n\r\n"));
bool bSendRequestSucceed = false;
if (WinHttpSendRequest(
hRequest,
m_additionalRequestHeaders.c_str(),
-1,
(LPVOID)pszTxt,
(DWORD)lstrlenA(pszTxt),
totalLength,
NULL))
{
bSendRequestSucceed = true;
}
DWORD dwWritten = 0;
unsigned int pack_size = 16384, sended = 0;
BYTE* pack = new BYTE[pack_size];
while (sended < fileLength)
{
unsigned int len = (sended + pack_size < fileLength) ? pack_size : fileLength - sended;
memcpy(pack, filebytes + sended, len);
if (!::WinHttpWriteData(hRequest,
pack,
len,
&dwWritten))
{
//m_dwLastError = ::GetLastError();
break;
}
sended += len;
}
delete[] pack;
char * endBoundary = "\r\n-----------------------------myfile--\r\n\r\n";
::WinHttpWriteData(hRequest,
endBoundary,
lstrlenA(endBoundary),
&dwWritten);
}
}
return 0;
}
char * GetFileBytes(string fileaddress, size_t& len) {
ifstream myfile(fileaddress);
if (myfile.is_open())
{
myfile.seekg(0, ios::end);
len = myfile.tellg();
char *ret = new char[len];
myfile.seekg(0, ios::beg);
myfile.read(ret, len);
myfile.close();
return ret;
}
else {
return NULL;
}
}
از دوستان کسی مشکلی در کد میبینه؟
وضعیت دریافتی در مقصد در برنامه :
header&mime2.jpg