1.GET方法
- pSession=new CInternetSession("Agent"); //User-Agent
- CHttpFile* pFile = NULL;
- CString strCookieData="name+xxxxxxxxxxxx";
- char PacketBuff[1024];
- char szBuff[1024]={0};
- DWORD dwRet;
- strcpy(GETaddr,"/sample/list.php");
- if(!pSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT,3600*1000/2) /
- ||!pSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT,3600*1000/2))//30min
- {
- MessageBox("SetOption Err!");
- }
- if(!pSession->GetCookie("http://172.16.11.2/xxx/","anchiva_avlab",strCookieData))
- {
- if(!pSession->SetCookie("http://172.16.11.2/xxx/","anchiva_avlab",strCookieData))
- {
- MessageBox("SetCookie False!");
- return FALSE;
- }
- }
- //MessageBox("GetCookie success!");
- pServer = pSession->GetHttpConnection("172.16.11.2");
- pFile = pServer->OpenRequest("GET", "/sample/request_task.php");
- // pFile->AddRequestHeaders(szHeader);
- pFile->SendRequest();
- pFile->QueryInfoStatusCode(dwRet);
- if (dwRet == HTTP_STATUS_OK)
- {
- while(UINT nRead=pFile->Read(PacketBuff, 1024-1))
- strncat(szBuff,PacketBuff,nRead);
- }
- else
- {
- MessageBox("HTTP_STATUS_ERR","HTTP连接错误!",MB_ICONERROR);
- ExitProcess(-1);
- }
- delete pFile;
- }
- catch ( CInternetException* e)
- {
- char ErrMsg[1024]={0};
- e->GetErrorMessage(ErrMsg,1024);
- ::MessageBox(NULL,ErrMsg,"Exception occur",MB_ICONERROR);
- ExitProcess(-1);
- }
2.POST方法:(可以post文件)
- try
- {
- //接收所有文件类型
- char accept[] = "*/*";
- //参数需要的数组
- const char *acArray[2] = {accept,NULL};
- LPCTSTR* ppstrAcceptTypes = acArray;
- pFile = pServer->OpenRequest("POST", "/sample/submit_sample.php",NULL,1,ppstrAcceptTypes,
- NULL,INTERNET_FLAG_EXISTING_CONNECT);
- pFile->AddRequestHeaders(strHeaders,HTTP_ADDREQ_FLAG_ADD);
- if(!pFile->SendRequestEx(size))
- {
- MessageBox(NULL,"SendRequest False!","Post",MB_OK);
- return;
- }
- // pFile->WriteString((char *)databuff);
- pFile->Write(databuff,size);
- pFile->EndRequest();
- pFile->QueryInfoStatusCode(dwRet);
- if (dwRet == HTTP_STATUS_OK)
- {
- while(UINT nRead=pFile->Read(PacketBuff, 1024-1))
- strncat(szBuff,PacketBuff,nRead);
- }
- else
- {
- MessageBox(NULL,"HTTP_STATUS_ERR","HTTP连接错误!",MB_ICONERROR);
- delete [] databuff;
- return;
- }
- delete pFile;
- }
- catch ( CInternetException* e)
- {
- char ErrMsg[1024]={0};
- e->GetErrorMessage(ErrMsg,1024);
- ::MessageBox(NULL,ErrMsg,"Exception occur",MB_ICONERROR);
- }