winInet編程

1.GET方法

 

  1. pSession=new CInternetSession("Agent"); //User-Agent 
  2.         CHttpFile* pFile = NULL;
  3.         CString strCookieData="name+xxxxxxxxxxxx";
  4.         char PacketBuff[1024];
  5.         char szBuff[1024]={0};
  6.         DWORD dwRet;
  7.         strcpy(GETaddr,"/sample/list.php");
  8.         if(!pSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT,3600*1000/2) /
  9.             ||!pSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT,3600*1000/2))//30min 
  10.         {
  11.             MessageBox("SetOption Err!");
  12.         }
  13.         if(!pSession->GetCookie("http://172.16.11.2/xxx/","anchiva_avlab",strCookieData))
  14.         {
  15.             if(!pSession->SetCookie("http://172.16.11.2/xxx/","anchiva_avlab",strCookieData))
  16.             {
  17.                 MessageBox("SetCookie False!");
  18.                 return FALSE;
  19.             }
  20.         }
  21.         //MessageBox("GetCookie success!"); 
  22.         pServer = pSession->GetHttpConnection("172.16.11.2");
  23.         
  24.         pFile = pServer->OpenRequest("GET""/sample/request_task.php");
  25.         //  pFile->AddRequestHeaders(szHeader); 
  26.         pFile->SendRequest();
  27.         pFile->QueryInfoStatusCode(dwRet);
  28.         
  29.         if (dwRet == HTTP_STATUS_OK)
  30.         {
  31.             while(UINT nRead=pFile->Read(PacketBuff, 1024-1))
  32.                 strncat(szBuff,PacketBuff,nRead);
  33.         }
  34.         else
  35.         {
  36.             MessageBox("HTTP_STATUS_ERR","HTTP連接錯誤!",MB_ICONERROR);
  37.             ExitProcess(-1);
  38.         }
  39.         delete pFile;
  40.     }
  41.     catch ( CInternetException* e)
  42.     {
  43.         char ErrMsg[1024]={0};
  44.         e->GetErrorMessage(ErrMsg,1024);
  45.         ::MessageBox(NULL,ErrMsg,"Exception occur",MB_ICONERROR);
  46.         ExitProcess(-1);
  47.     }

 

2.POST方法:(可以post文件)

  1. try
  2. {
  3.     //接收所有文件類型
  4.     char accept[] = "*/*";
  5.     //參數需要的數組
  6.     const char *acArray[2] = {accept,NULL};
  7.     LPCTSTR* ppstrAcceptTypes = acArray;
  8.     pFile = pServer->OpenRequest("POST""/sample/submit_sample.php",NULL,1,ppstrAcceptTypes,
  9.         NULL,INTERNET_FLAG_EXISTING_CONNECT);
  10.     pFile->AddRequestHeaders(strHeaders,HTTP_ADDREQ_FLAG_ADD);
  11.     
  12.     if(!pFile->SendRequestEx(size))
  13.     {
  14.         MessageBox(NULL,"SendRequest False!","Post",MB_OK);
  15.         return;
  16.     }
  17.     //  pFile->WriteString((char *)databuff);  
  18.     pFile->Write(databuff,size);    
  19.     pFile->EndRequest();
  20.     
  21.     pFile->QueryInfoStatusCode(dwRet);
  22.     if (dwRet == HTTP_STATUS_OK)
  23.     {
  24.         while(UINT nRead=pFile->Read(PacketBuff, 1024-1))
  25.                 strncat(szBuff,PacketBuff,nRead);
  26.     }
  27.     else
  28.     {
  29.         MessageBox(NULL,"HTTP_STATUS_ERR","HTTP連接錯誤!",MB_ICONERROR);
  30.         delete [] databuff;
  31.         return;
  32.     }
  33.     delete pFile;
  34. }
  35. catch ( CInternetException* e)
  36.     {
  37.         char ErrMsg[1024]={0};
  38.         e->GetErrorMessage(ErrMsg,1024);
  39.         ::MessageBox(NULL,ErrMsg,"Exception occur",MB_ICONERROR);
  40.     }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章