我通过表单上传文件
但我在提交前要login, 使得我的表单不能提交,,, 怎么办啊兄弟们,帮帮我啊。。急死我了。
我可以从返回的信息内得到Set-Cookie: JSESSIONIDXXXXXXXXXXXXXXXXXXX; Path=/EIP
部分代码如下:
但我在提交前要login, 使得我的表单不能提交,,, 怎么办啊兄弟们,帮帮我啊。。急死我了。
我可以从返回的信息内得到Set-Cookie: JSESSIONIDXXXXXXXXXXXXXXXXXXX; Path=/EIP
部分代码如下:
CUploader uploader;
UploadSettings Settings; Settings.AddField( "navMode", "I" );
Settings.AddField( "action", "login" );
Settings.AddField( "loginName", "holmes" );
Settings.AddField( "loginPwd", "password" );
//Settings.AddFile( "uploadFile", "C:\\DataImportTemplate1.xls", _T("application/octet-stream") );
Settings.SetAddress( "http://127.0.0.1/EIP/Login.do" );
uploader.DoUpload( &Settings ); AfxMessageBox( "OK" );
Settings.ClearFields();
Settings.SetAddress( "http://127.0.0.1/EIP/jsp/odma/checkoutDocument.jsp" );
uploader.DoUpload( &Settings );
CUploader::DoUpload( UploadSettings *pSettings )
{
//m_pSettings = pSettings;
//FieldInfoCollection fields;
//m_pSettings->GetFields(&fields); ////for (FieldInfoCollection::const_iterator i = fields.begin(); i != fields.end(); ++i)
//{
// //AfxMessageBox( (LPCSTR)i->GetFieldName().data() );
// ::MessageBox( NULL, (LPCSTR)_T(i->GetFieldName().data()), "(LPCSTR)i->GetFieldValue()data()", MB_OK );
// //HRESULT hr = UploadField(hRequest, strBoundary, i->GetFieldName(), i->GetFieldValue());
//
//}
//return 0;
m_pSettings = pSettings;
LPCTSTR lpszAgent = _T("WinInetPost/0.0.4");
DWORD dwOpenInternetFlags = 0;//INTERNET_FLAG_ASYNC;
HINTERNET hInternet = InternetOpen( lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL,
NULL, dwOpenInternetFlags );
if (!hInternet)
{
char buff[100];
wsprintf( buff, "InternetOpen failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
MessageBox( NULL, buff, "Error Upload", MB_OK );
return HRESULT_FROM_WIN32(GetLastError());
} MessageBox( NULL, m_pSettings->GetHostName().data(), "hostName", MB_OK ); INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT;
LPCTSTR lpszUserName = NULL;
LPCTSTR lpszPassword = NULL;
DWORD dwConnectFlags = 0;
DWORD_PTR dwConnectContext = reinterpret_cast<DWORD_PTR>(this);
string ss; HINTERNET hConnect = InternetConnect( hInternet,
( LPCSTR)m_pSettings->GetHostName().data(),
8080,
lpszUserName, lpszPassword,
INTERNET_SERVICE_HTTP,
dwConnectFlags, dwConnectContext);
if (!hConnect)
{
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff, "InternetConnect failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
MessageBox( NULL, buff, "Error Upload", MB_OK );
return HRESULT_FROM_WIN32(GetLastError());
} LPCTSTR lpszVerb = _T( "POST" );
LPCTSTR lpszVersion = NULL; // Use default.
LPCTSTR lpszReferrer = NULL; // No referrer.
LPCTSTR *lplpszAcceptTypes = NULL; // Whatever the server wants to give us.
DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_RELOAD;
DWORD_PTR dwOpenRequestContext = reinterpret_cast<DWORD_PTR>(this);
HINTERNET hRequest = HttpOpenRequest( hConnect, lpszVerb, (LPCSTR)m_pSettings->GetUrlPath().data(), lpszVersion,
lpszReferrer, lplpszAcceptTypes,
dwOpenRequestFlags, dwOpenRequestContext);
if (!hRequest)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff, "HttpOpenRequest failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
MessageBox( NULL, buff, "Upload Error", MB_OK );
return HRESULT_FROM_WIN32(GetLastError());
} string strBoundary = GenerateMultipartBoundary();
CString str( strBoundary.c_str() );
string contentTypeHeader = GenerateContentTypeHeader(strBoundary); //MessageBox( NULL, "EndRequest", "Error Upload", MB_OK );
CString strbuff;//( (LPCSTR)contentTypeHeader.data() + "===");
strbuff.Format( "==%s===", (LPCSTR)contentTypeHeader.data() );
AfxMessageBox( strbuff );
BOOL bResult = HttpAddRequestHeaders(hRequest, (LPCSTR)contentTypeHeader.data(), -1, HTTP_ADDREQ_FLAG_ADD);
if (!bResult)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff, "HttpAddRequestHeaders failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
return HRESULT_FROM_WIN32(GetLastError());
} DWORD dwOverallBytesTotal = 0;
DWORD contentLength = CalculateContentLength(strBoundary, &dwOverallBytesTotal); INTERNET_BUFFERS buffersIn;
memset(&buffersIn, 0, sizeof(INTERNET_BUFFERS));
buffersIn.dwStructSize = sizeof(INTERNET_BUFFERS);
buffersIn.dwBufferTotal = contentLength; bResult = HttpSendRequestEx(hRequest, &buffersIn, NULL, HSR_INITIATE, reinterpret_cast<DWORD_PTR>(this));
if (!bResult)
{
// If GetLastError() == 6 (ERROR_INVALID_HANDLE), then this often means that
// the server wasn't actually up. Unfortunately, I don't know of a better
// way to get more information.
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff,"HttpSendRequestEx failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
return HRESULT_FROM_WIN32(GetLastError());
} FieldInfoCollection fields;
m_pSettings->GetFields(&fields); for (FieldInfoCollection::const_iterator i = fields.begin(); i != fields.end(); ++i)
{
HRESULT hr = UploadField( hRequest, strBoundary, (LPCTSTR)i->GetFieldName().data(),
(LPCTSTR)i->GetFieldValue().data());
if (FAILED(hr))
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return hr;
}
} FileInfoCollection files;
m_pSettings->GetFiles(&files); DWORD dwOverallBytesSent = 0;
for (FileInfoCollection::const_iterator i = files.begin(); i != files.end(); ++i)
{
HRESULT hr = UploadFile( hRequest, strBoundary, (LPCTSTR)i->GetFieldName().data(),
(LPCTSTR)i->GetPathName().data(),
(LPCTSTR)i->GetContentType().data(),
&dwOverallBytesSent, dwOverallBytesTotal );
if (FAILED(hr))
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return hr;
}
} // After the last file:
string bodyTrailer = GenerateBodyTrailer(strBoundary);
DWORD dwBytesWritten;
bResult = InternetWriteFile( hRequest, (LPCVOID)(bodyTrailer.data()),
(DWORD)bodyTrailer.size(), &dwBytesWritten );
if (!bResult)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff, "InternetWriteFile failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
MessageBox( NULL, buff, "Error Upload", MB_OK );
return HRESULT_FROM_WIN32(GetLastError());
}
bResult = HttpEndRequest(hRequest, NULL, 0, reinterpret_cast<DWORD_PTR>(this));
if (!bResult)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
char buff[100];
wsprintf( buff, "HttpEndRequest failed, error = %d (0x%x)\n", GetLastError(), GetLastError());
MessageBox( NULL, buff, "Error Upload", MB_OK );
// TODO: return S_OK anyway? It's probably too late to do anything about it now.
return HRESULT_FROM_WIN32(GetLastError());
}
TCHAR lpszCookieData[256];
DWORD lpdwSize = sizeof( lpszCookieData );
InternetGetCookie( "http://127.0.0.1:8080/EIP/Login.do", NULL, lpszCookieData, &lpdwSize) ;
MessageBox( NULL, lpszCookieData, "asdfdddddddd", MB_OK ) ; DWORD dwStatusCode;
ReadStatusCode(hRequest, &dwStatusCode);
MessageBox( NULL, "Re", "asdfdddddddd", MB_OK ) ;
ReadResponseHeaders(hRequest);
ReadResponseBody(hRequest);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 0;
}
Upload之前set一下login得到的Cookie