首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

http下载数据的例子 请

2012-05-05 
http下载数据的例子 请高手指点啊这是一个修改微软SDK的例子 在HttpSendRequest函数处老是出错 高手看看吧

http下载数据的例子 请高手指点啊
这是一个修改微软SDK的例子 在HttpSendRequest函数处老是出错 高手看看吧 谢谢了

int WINAPI WinMain( HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPTSTR lpCmdLine,
  int nCmdShow)
{

BOOL bReturn = FALSE;
CHAR lpszServer[64] = "http://www.baidu.com/";

  HINTERNET hOpen = NULL, 
  hConnect = NULL, 
  hRequest = NULL;

  DWORD dwSize = 0, 
  dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE; 

  TCHAR szErrMsg[200];
  
  char *lpBufferA,
  *lpHeadersA;

  TCHAR *lpBufferW,
  *lpHeadersW;

  LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL}; 
  LPCWSTR temp = TEXT("123456");
 
  MessageBox(NULL,temp,NULL,NULL);
  // Initialize the use of the Windows CE Internet functions.
  if (0)
  {
  hOpen = InternetOpen(TEXT("CeHttp"), INTERNET_OPEN_TYPE_PROXY, 
  NULL, 0, 0);
  }
  else
  {
  hOpen = InternetOpen(TEXT("CeHttp"), INTERNET_OPEN_TYPE_DIRECT,
  NULL, 0, 0);
  }

  if (!hOpen)
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpen Error"), 
  GetLastError());
  return FALSE;
  }
  
  if (0)
  {
  if (!(hRequest = InternetOpenUrl (hOpen, TEXT(" http://www.baidu.com/"), NULL, 0, 
  INTERNET_FLAG_RELOAD, 0)))
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpenUrl Error"),
  GetLastError());
  goto exit;
  }
  }
  else
  {
  // Open an HTTP session for a specified site by using lpszServer. 
  if (!(hConnect = InternetConnect (hOpen, 
TEXT("www.baidu.com"), 
  INTERNET_DEFAULT_HTTP_PORT, 
  NULL, NULL, 
  INTERNET_SERVICE_HTTP, 
  0, 0)))
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
  GetLastError());
  goto exit;
  }

  // Open an HTTP request handle. 
  if (!(hRequest = HttpOpenRequest (hConnect, 
  TEXT("GET"), 
  NULL, 
  HTTP_VERSION, 
  NULL, 
  (LPCTSTR*)AcceptTypes, 
  dwFlags, 0)))
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpOpenRequest Error"),
  GetLastError());
  goto exit;
  }
  // Send a request to the HTTP server. 
  if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0))///////////////////////////////出错处
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpSendRequest Error"),
  GetLastError());
  goto exit;
  }
  }
  


  // Call HttpQueryInfoAto find out the size of the headers.
  HttpQueryInfoA(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
  NULL);

  // Allocate a block of memory for lpHeadersA.
  lpHeadersA = new CHAR [dwSize];
  //if memory allocation fails, then free the block of memory.
  if(!lpHeadersA)
  {
  delete[] lpHeadersA;
  goto exit;
  }
  // Call HttpQueryInfoA again to get the headers.
  if (!HttpQueryInfoA(hRequest, 
  HTTP_QUERY_RAW_HEADERS_CRLF, 
  (LPVOID) lpHeadersA, &dwSize, NULL))
  {
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpQueryInfo"), 
  GetLastError());
  goto exit;
  }
  else
  {
  // Clear all of the existing text in the edit control and prepare 
  // to put the new information in it.
  // SendMessage (HWND_BROADCAST, EM_SETSEL, 0, -1);
  // SendMessage (HWND_BROADCAST, WM_CLEAR, 0, 0);
  // SendMessage (HWND_BROADCAST, WM_PAINT, TRUE, 0);
  }

  // Terminate headers with NULL.
  lpHeadersA [dwSize] = '\0';
  
  // Get the required size of the buffer that receives the Unicode 
  // string. 
  dwSize = MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, NULL, 0);
  
  // Allocate a block of memory for lpHeadersW.
  lpHeadersW = new TCHAR [dwSize];
  //if memory allocation fails, then free the block of memory.
  if(!lpHeadersW)
  {
  delete[] lpHeadersW;
  goto exit;
  }

  // Convert headers from ASCII to Unicode.
  MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, lpHeadersW, dwSize);  
  
  // Put the headers in the edit control.
 // SendMessage (HWND_BROADCAST, WM_PUTTEXT, NULL, (LPARAM) lpHeadersW);
 // SendMessage (HWND_BROADCAST, NULL, NULL, (LPARAM) lpHeadersW);
  // Free the blocks of memory.
  delete[] lpHeadersA;
  delete[] lpHeadersW;

  // Allocate a block of memory for lpHeadersW.
  lpBufferA = new CHAR [32000];
  //if memory allocation fails, then free the block of memory.
  if(!lpBufferA)
  {
  delete[] lpBufferA;
  goto exit;
  }

  do
  {
  if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, 31999, &dwSize))
  {
  wsprintf(szErrMsg, TEXT("%s: %x"), TEXT("InternetReadFile Error"), 
  GetLastError());
  goto exit;
  }

  if (dwSize != 0)  
  {
  // Terminate headers with NULL.
  lpBufferA [dwSize] = '\0';
  LPCWSTR temp = TEXT("78910");

  // Get the required size of the buffer which receives the Unicode
  // string. 
  dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);
   
  // Allocate a block of memory for lpBufferW.
  lpBufferW = new TCHAR [dwSize];
  MessageBox(NULL,(LPCWSTR)lpBufferW,NULL,NULL);

  //if memory allocation fails, then free the block of memory.
  if(!lpBufferW)
  {
  delete[] lpBufferW;
  goto exit;
  }

  // Convert the buffer from ASCII to Unicode.
  MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize); 


MessageBox(NULL,(LPCWSTR)*lpBufferW,NULL,NULL);///////////////////////////////


  // Put the buffer in the edit control.
  //SendMessage (HWND_BROADCAST, WM_PUTTEXT, NULL, (LPARAM) lpBufferW); 
//SendMessage (HWND_BROADCAST, NULL, NULL, (LPARAM) lpHeadersW);



  // Free the block of memory.
  delete[] lpBufferW;  
  }  
  } while (dwSize);

  // Free the block of memory.
  delete[] lpBufferA;  

  bReturn = TRUE;

exit:

  // Close the Internet handles.
  if (hOpen)
  {
  if (!InternetCloseHandle (hOpen))
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
  GetLastError());
  }
  
  if (hConnect)
  {
  if (!InternetCloseHandle (hConnect))
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
  GetLastError());
  }

  if (hRequest)
  {
  if (!InternetCloseHandle (hRequest))
  wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"), 
  GetLastError());
  }

  return bReturn;

}


[解决办法]

[解决办法]

C/C++ code
AfxParseURL(m_myURL,servicetype,server,object,port);pHttpRequest->m_hConnection=InternetConnect(m_hRoot,server,port,                    NULL,NULL,servicetype,0,0);m_hOpen=HttpOpenRequest(m_hConnection,L"POST",object,L"HTTP/1.1",NULL,(LPCTSTR*)AcceptTypes,INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_DONT_CACHE,0);
[解决办法]
你连接上网络了吗?

热点排行