wince 中使用HttpSendRequest函数POST数据时应该注意的一点

测试用例代码

 CString szHeaders;
 szHeaders ="EmployeeID: "+UserID+"/r/n";
 szHeaders+="EmployeePassword: "+Password+"";
 
// HTTPRequest Response;
// memset(&Response, NULL, sizeof(HTTPRequest));
// if (!MCHttp.SendRequestEx(m_VerbType, szURL, HTTP_VERSION_1_1, TEXT("*/*"), TEXT("PervasiveBiz"), szHeaders, 
//      szSendBuffer, 60000, &Response))
 CString strServerName;
 CString strObject;
 INTERNET_PORT nPort;
 DWORD dwServiceType; 
 if (!AfxParseURL(szURL, dwServiceType, strServerName, strObject, nPort) || 
  dwServiceType != AFX_INET_SERVICE_HTTPS && dwServiceType != AFX_INET_SERVICE_HTTP)
 {
  return -2;
 }

 HINTERNET m_hOpen; 
    HINTERNET m_hConnect;
    HINTERNET m_hRequest;  
 m_hOpen = InternetOpen(TEXT("PervasiveBiz"), INTERNET_OPEN_TYPE_DIRECT,NULL, 0, 0);
 if (!m_hOpen)
 {
  return -1;
 }
 
 if (!(m_hConnect = InternetConnect(m_hOpen, strServerName,  nPort,  NULL, NULL, dwServiceType,  0, 0)))
 {
  InternetCloseHandle(m_hOpen);
  return -1;
 }
 
 LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};
 DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
 // Open an HTTP request handle.

 if (!(m_hRequest = HttpOpenRequest (m_hConnect, TEXT("POST"), strObject, HTTP_VERSION, NULL, (LPCTSTR*)AcceptTypes,
   dwFlags, 0)))
 {
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  return -1;
 }
 
 DWORD dwHttpTimeout = TIME_OUT;
 if(!InternetSetOption(m_hRequest,INTERNET_OPTION_CONNECT_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  return -1;
 }
 if(!InternetSetOption(m_hRequest,INTERNET_OPTION_SEND_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  return -1;
 }
 if(!InternetSetOption(m_hRequest,INTERNET_OPTION_RECEIVE_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);;
  return -1;
 }

// MessageBox(NULL,  szSendBuffer,szHeaders, 0);

////////////////////////////////////////////////重点要注意的地方,HttpSendRequest的第三个参数必须为多字节编码格式,否则服务器端会出错的。花了一天半的时间调试出来的结论。如果不进行转换,服务器端只会接收一半数据,后面的数据将会收不到。
 byte* pszData = new byte[szSendBuffer.GetLength()];
 char *pConten = new char[szSendBuffer.GetLength()];
 memset(pszData,0,szSendBuffer.GetLength());
 memset(pConten,0,szSendBuffer.GetLength());
 WideCharToMultiByte( CP_ACP, 0, szSendBuffer.GetBuffer(0),
   -1, pConten, szSendBuffer.GetLength(), NULL, NULL );
 memcpy(pszData, pConten, szSendBuffer.GetLength());
 
 int len = strlen((char*)pszData);
 int len2 = szSendBuffer.GetLength();
 //MessageBox(NULL, szSendBuffer, str);
 if(HttpSendRequest(m_hRequest, szHeaders, szHeaders.GetLength(), pszData, szSendBuffer.GetLength())
   == FALSE)
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  return -1;  
 }

 DWORD dwSize;
 HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,NULL);

 TCHAR* lpHeaders = new TCHAR [dwSize];
 if (!HttpQueryInfo (m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpHeaders, &dwSize, NULL))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders;
  return -1;
 }
 
// MessageBox(NULL, lpHeaders, _T("HttpQueryInfo"),  0);

 TCHAR szHttpStatus[4] = {0};
 memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
 int dwHttpStatus = 0;
 dwHttpStatus =_wtoi(szHttpStatus);
 if ((dwHttpStatus<200)||(dwHttpStatus>300))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders;
  return -1;
 }

 if(!InternetReadFile (m_hRequest, (LPVOID)(cResponse),32*1024, &dwSize))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders; 

  return -1; 
 }
 if(dwSize == 0)
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders; 
  return -1;  
 }
 

 HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,NULL);
 if (!HttpQueryInfo (m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, 
   (LPVOID) lpHeaders, &dwSize, NULL))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders; 
  return -1;
 }
 
 memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
 dwHttpStatus = _wtoi(szHttpStatus);
 if ((dwHttpStatus<200)||(dwHttpStatus>300))
 {
  InternetCloseHandle(m_hRequest);
  InternetCloseHandle(m_hConnect);
  InternetCloseHandle(m_hOpen);
  delete[] lpHeaders;
  return -1;
 }

// MessageBox(NULL, lpHeaders, _T("lpHeaders"),  0);
 delete[] lpHeaders; 
 InternetCloseHandle(m_hRequest);
 InternetCloseHandle(m_hConnect);
 InternetCloseHandle(m_hOpen);
 return SENDDATA_SCUCCEED ;


原文:http://blog.csdn.net/zjh824/article/details/1845580

你可能感兴趣的:(wince 中使用HttpSendRequest函数POST数据时应该注意的一点)