Reputation: 5
I'm using WinINet class in MFC to upload files to webserver. When I run this program, httpSendRequestEx returns 12005 error which is invalid url. If I put http:// in front of the url, httpSendRequestEx returns 12007 error which is "The server name could not be resolved". I spent whole day to fix this problem, but I have no idea how to fix it. Please help me
Here is my code.
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);
}
Upvotes: 0
Views: 600
Reputation: 37122
InternetConnect
accepts only the server name/address, and the port.
hConnection = ::InternetConnect(hInternet, "192.168.88.4", 8090, ...);
That's because InternetConnect
only makes a connection to a server (a port on a machine somewhere). The URL forms part of the request to the server, so the additional string ("/mrbs/controller?do=XML_MULTIPART_TEST_ACTION") you were passing to InternetConnect
actually needs to be given to HttpOpenRequest
.
hRequest = ::HttpOpenRequest(hConnection, "POST", "/mrbs/controller?do=XML_MULTIPART_TEST_ACTION", ...);
I'm not sure where the "/jsp/inc/head.jsp" that you were originally passing to HttpOpenRequest
fits in, but if that really is part of the URL too then you would do this:
hRequest = ::HttpOpenRequest(hConnection, "POST", "/mrbs/controller?do=XML_MULTIPART_TEST_ACTION/jsp/inc/head.jsp", ...);
Upvotes: 1