在MFC中使用WinINet的问题

时间:2015-08-20 01:46:33

标签: c++ url mfc wininet

我在MFC中使用WinINet类将文件上传到网络服务器。 当我运行此程序时,httpSendRequestEx返回12005错误,这是无效的URL。 如果我将http://放在url前面,httpSendRequestEx将返回12007错误,即"服务器名称无法解析"。 我花了一整天来解决这个问题,但我不知道如何解决它。 请帮帮我

这是我的代码。

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);
}

1 个答案:

答案 0 :(得分:1)

InternetConnect仅接受服务器名称/地址和端口。

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

那是因为InternetConnect只与服务器(某个机器上的某个端口)建立连接。 URL构成了对服务器的请求的一部分,因此您传递给InternetConnect实际需要的附加字符串(" / mrbs / controller?do = XML_MULTIPART_TEST_ACTION" )被赋予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", ...);