标签:debug object close one har blog adl 错误 参数错误
BOOL UploadFile(LPCTSTR strURL, LPCTSTR strLocalFileName) { if (strURL == NULL || strURL == _T("") || strLocalFileName == NULL || strLocalFileName == _T("")) { return FALSE; } DWORD dwReadLength; DWORD dwResponseLength; DWORD dwTotalRequestLength; const DWORD dwChunkLength = 1024; char pBuffer[dwChunkLength]; memset(pBuffer, 0, dwChunkLength); CHttpFile *pHttpFile = NULL; CHttpConnection* connection = NULL; CInternetSession session(_T("UploadFile")); DWORD dwType; INTERNET_PORT nPort; CString strServer, strObject; if (!AfxParseURL(strURL, dwType, strServer, strObject, nPort)) { #ifdef DEBUG OutputDebugString(_T("URL参数错误!")); #endif // DEBUG return FALSE; } try { connection = session.GetHttpConnection(strServer, nPort); if (connection == NULL) { #ifdef DEBUG OutputDebugString(_T("获取连接出错!")); #endif // DEBUG session.Close(); return FALSE; } pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject); if (pHttpFile == NULL) { #ifdef DEBUG OutputDebugString(_T("获取请求失败!")); #endif // DEBUG connection->Close(); connection = NULL; session.Close(); return FALSE; } CTime ct; CString strTime, strBoundary; ct = CTime::GetCurrentTime(); strTime = ct.Format(_T("%Y%m%d%H%M%S")); strBoundary = _T("*-*-*") + strTime + _T("*-*-*"); CString strPreFileHeaders; strPreFileHeaders.Format(_T("Content-Type: multipart/form-data; boundary=%s\r\n"), strBoundary); pHttpFile->AddRequestHeaders(strPreFileHeaders); CString strPostData; strPostData.Format(_T("--%s\r\nContent-Disposition: form-data; name =\"voice\"; filename=\"%s.wav\"\r\nContent-Type: audio/x-wav\r\n\r\n"), strBoundary, strTime); CString endPostData; endPostData.Format(_T("\r\n--%s--\r\n"), strBoundary); CFile file; if (!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead)) { #ifdef DEBUG OutputDebugString(_T("打开文件失败!")); #endif // DEBUG pHttpFile->Close(); pHttpFile = NULL; connection->Close(); connection = NULL; session.Close(); return FALSE; } DWORD sendLength = strPostData.GetLength()+endPostData.GetLength()+file.GetLength(); if (!pHttpFile->SendRequestEx(sendLength, HSR_SYNC | HSR_INITIATE)) { #ifdef DEBUG OutputDebugString(_T("发送请求失败!")); #endif // DEBUG pHttpFile->Close(); pHttpFile = NULL; connection->Close(); connection = NULL; session.Close(); return FALSE; } pHttpFile->Write((LPSTR)(LPCTSTR)strPostData, strPostData.GetLength()); dwReadLength = -1; while (0 != dwReadLength) { dwReadLength = file.Read(pBuffer, dwChunkLength); if (dwReadLength > 0) { pHttpFile->Write(pBuffer, dwReadLength); } } file.Close(); pHttpFile->Write((LPSTR)(LPCTSTR)endPostData, endPostData.GetLength()); pHttpFile->EndRequest(); LPSTR szResponse; CString strResponse; dwResponseLength = pHttpFile->GetLength(); while (0 != dwResponseLength) { szResponse = (LPSTR)malloc(dwResponseLength + 1); szResponse[dwResponseLength] = ‘\0‘; pHttpFile->Read(szResponse, dwResponseLength); strResponse += szResponse; free(szResponse); dwResponseLength = pHttpFile->GetLength(); } #ifdef DEBUG OutputDebugString(strResponse); #endif // DEBUG AfxMessageBox(strResponse); if (pHttpFile != NULL) { pHttpFile->Close(); pHttpFile = NULL; } if (connection != NULL) { connection->Close(); connection = NULL; } session.Close(); return TRUE; } catch (CInternetException * pEx) { char sz[256]; pEx->GetErrorMessage(sz, 25); CString str; str.Format("InternetException occur!\r\n%s", sz); AfxMessageBox(str); } catch (...) { } if (pHttpFile != NULL) { pHttpFile->Close(); pHttpFile = NULL; } if (connection != NULL) { connection->Close(); connection = NULL; } session.Close(); return FALSE; }
标签:debug object close one har blog adl 错误 参数错误
原文地址:http://www.cnblogs.com/mupiaomiao/p/6322560.html