在 MFC 中使用 WinINet 的问题

problems using WinINet in MFC

我在 MFC 中使用 WinINet class 将文件上传到网络服务器。 当我运行这个程序时,httpSendRequestEx returns 12005 错误是无效的url。 如果我将 http:// 放在 url 前面,httpSendRequestEx returns 12007 错误即 "The server name could not be resolved"。 我花了一整天的时间来解决这个问题,但我不知道如何解决。 请帮助我

这是我的代码。

CString m_strBoundary = ("abcdefg");
LPCTSTR lpszFilePath = "C:\MFCprj\WinINet_Example2\WinINet_Example2\Full.jpg";
LPCTSTR lpszFileName = "Full.jpg";;
HANDLE hFile = CreateFile("C:\MFCprj\WinINet_Example2\WinINet_Example2\Full.jpg", GENERIC_READ, 0, 0, 0, 0, 0);

HINTERNET hInternet = ::InternetOpen("WININETEXAMPLE", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hInternet)
{
    AfxMessageBox("InternetOpen has an error");
}

HINTERNET hConnection = ::InternetConnect(hInternet, "192.168.88.4:8090/mrbs/controller?do=XML_MULTIPART_TEST_ACTION", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL); 
if(!hConnection)                                      
{
    AfxMessageBox("InternetConnect has an error");
}

HINTERNET hRequest = ::HttpOpenRequest(hConnection, "POST", "/jsp/inc/head.jsp", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if(!hRequest)
{
    AfxMessageBox("HttpOpenRequest has an error");
}

//HTTP header
_bstr_t strHeader;
strHeader = "Content-Type: multipart/form-data; boundary=";
strHeader = strHeader + m_strBoundary;
::HttpAddRequestHeaders(hRequest, (LPCTSTR)strHeader, -1, 0);

//Body header
_bstr_t strFilePartHead("--");
strFilePartHead = strFilePartHead + m_strBoundary;
strFilePartHead = strFilePartHead + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Full.jpg\"";
strFilePartHead = strFilePartHead + "\"\r\nContent-Type: application/vnd.ms-excel\r\n\r\n";

_bstr_t strFilePartTail("\r\n");
strFilePartTail = strFilePartTail + "--";
strFilePartTail = strFilePartTail + m_strBoundary;
strFilePartTail = strFilePartTail + "--\r\n";

INTERNET_BUFFERS BufferIn;
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.Next = NULL;
BufferIn.lpcszHeader = (LPCTSTR)strHeader;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = GetFileSize(lpszFilePath) + strlen( (LPCSTR)strFilePartHead ) + strlen( (LPCSTR)strFilePartTail );
BufferIn.dwHeadersLength = lstrlen( (LPCTSTR)strHeader );
BufferIn.dwHeadersTotal = lstrlen( (LPCTSTR)strHeader );
BufferIn.lpvBuffer = NULL;
BufferIn.dwOffsetHigh = 0;
BufferIn.dwOffsetLow = 0;

if(!::HttpSendRequestEx(hRequest, &BufferIn, NULL, 0, NULL))
{
    DWORD dwError = GetLastError();
    ::InternetCloseHandle(hRequest);
    ::InternetCloseHandle(hConnection);
    ::InternetCloseHandle(hInternet);
}

InternetConnect 仅接受服务器 name/address 和端口。

hConnection = ::InternetConnect(hInternet, "192.168.88.4", 8090, ...);

那是因为 InternetConnect 只连接到服务器(某台机器上的端口)。 URL 构成对服务器请求的一部分,因此您传递给的附加字符串 ("/mrbs/controller?do=XML_MULTIPART_TEST_ACTION") InternetConnect实际上需要给HttpOpenRequest

hRequest = ::HttpOpenRequest(hConnection, "POST", "/mrbs/controller?do=XML_MULTIPART_TEST_ACTION", ...);

我不确定您最初传递给 HttpOpenRequest"/jsp/inc/head.jsp" 放在哪里,但是如果那确实也是 URL 的一部分,那么您可以这样做:

hRequest = ::HttpOpenRequest(hConnection, "POST", "/mrbs/controller?do=XML_MULTIPART_TEST_ACTION/jsp/inc/head.jsp", ...);