USES_CONVERSION;
INTERNET_DATA internetData;
DWORD dwAccessType = 0;
dwAccessType |= INTERNET_OPEN_TYPE_PRECONFIG;
LPCSTR userName = "someusername";
LPCSTR password = "somepassword";
internetData.hOpen = InternetOpen(_T("agentname"),
dwAccessType,
NULL,
NULL,
0);
if (internetData.hOpen != NULL)
{
internetData.hConnect = InternetConnectA(internetData.hOpen,
W2A(L"webserverIP"),
port,
userName,
password,
INTERNET_SERVICE_HTTP,
0,
0);
if (internetData.hConnect != NULL)
{
cout << "hconnect is not null" << "\n";
}
}
internetData.hRequest = HttpOpenRequestA(internetData.hConnect,
W2A(L"POST"),
"api/photo",
"HTTP/1.1",
NULL,
NULL,
NULL,
0);
HttpAddRequestHeadersA(internetData.hRequest, authInfo.c_str(),
authInfo.size(), HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA);
CString upFile = "temp.jpg";
CString m_sBoundary = "---------------------------8d611c5f734a415";
LPCSTR sHeader = "Content-Type: multipart/form-data; boundary=-----------
----------------8d611c5f734a415\r\n";
HttpAddRequestHeadersA(internetData.hRequest,
sHeader,
lstrlenA(sHeader),
HTTP_ADDREQ_FLAG_ADD);
LPCSTR extraHeaders = "Accept:application/json";
HttpAddRequestHeadersA(internetData.hRequest,
extraHeaders,
lstrlenA(extraHeaders),
HTTP_ADDREQ_FLAG_ADD_IF_NEW);
LPCSTR sTail = "\r\n---------------------------8d611c5f734a415\r\nContent-Disposition: form-data; name=\"image[file]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
CString Tail = L"--";
Tail += m_sBoundary;
Tail += L"--\r\n";
HANDLE hFile = CreateFile(L"E:\\temp.jpg", GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
cout << "Invalid File Handle\n";
}
INTERNET_BUFFERS BufferIn = { 0 };
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.dwBufferTotal = lstrlenA(sTail) + GetFileSize(hFile, NULL) + Tail.GetLength();
cout << BufferIn.dwBufferTotal << "\n";
if (!HttpSendRequestEx(internetData.hRequest, &BufferIn,NULL, HSR_INITIATE, 0))
{
printf("Error on HttpSendRequestEx %d\n", GetLastError());
cout << "request ex failed\n";
}
DWORD sum = 0;
unsigned long ulWrote = 0;
BOOL bWrote = false;
//stream header
bWrote = InternetWriteFile(internetData.hRequest, (const void*)sTail, lstrlenA(sTail), &ulWrote);
sum += ulWrote;
if (!bWrote)
{
cout << "Stream Header Failed\n";
}
DWORD dwBytesRead;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead; // stream binary file
do
{
if (!(bRead = ReadFile(hFile, pBuffer, sizeof(pBuffer),&dwBytesRead, NULL)))
{
cout << "Faile read failed\n";
}
if (!(bRead = InternetWriteFile(internetData.hRequest, pBuffer, dwBytesRead, &ulWrote)))
{
cout << "Stream write failed\n";
}
sum += ulWrote;
} while (dwBytesRead == sizeof(pBuffer));
bWrote = InternetWriteFile(internetData.hRequest, (const void*)Tail, Tail.GetAllocLength(), &ulWrote);
sum += ulWrote;
if (!bWrote)
{
cout << "Taile failed\n";
}
CloseHandle(hFile);
cout << sum << "\n";
if (!HttpEndRequest(internetData.hRequest, NULL, HSR_INITIATE, 0))
{
if (!HttpEndRequest(internetData.hRequest, NULL, 0, 0))
{
cout << GetLastError() << "\n";
}
}
DWORD nRead = 0L;
char inbuf[1024];
do
{
nRead = 0L;
bRead = InternetReadFile(internetData.hRequest, inbuf, 1023, &nRead);
if (nRead > 0)
{
inbuf[nRead] = '\0';
CString aText = inbuf;
cout << inbuf << "\n";
}
} while (bRead == TRUE && nRead > 0);
程序已成功编译。从服务器端运行时,我收到错误的请求错误。
在服务器端,image [file]属性中未收到文件。此代码有什么问题?代码是否有错误?我正在努力争取两天的问题...任何人都可以帮助获得想法。