TIdHttp C++有没有类似的类?解决方案
TIdHttp C++有没有类似的类?
最近把一个dephi 的程序翻译成C++的,一直没做过网络方面的东西。
请教一下,TIdHttp这个类,在MFC有没有相同功能的?
或者说我用c++要怎么Post数据过去?
aStream 这个结果又怎么取回来?
谢谢
------解决方案--------------------
refer to WinHttp class, function SendRequest()
------解决方案--------------------
直接上代码
最近把一个dephi 的程序翻译成C++的,一直没做过网络方面的东西。
function PostData(AIP,AParam: String;var sRst: String): Boolean;
var
PostURL, sParams: String;
aParams: TStringList;
aStream: TStringStream;
IdHttp: TIdHttp;
begin
Result := False;
IdHttp := TIdHttp.Create(nil);
aParams := TStringList.Create;
aStream := TStringStream.Create('');
PostURL := 'http://'+Trim(AIP)+'/oss/client/analysis.g'+AParam;
sParams := ''
try
try
aParams.Clear;
aParams.Add(sParams);
IdHttp.Request.ContentType := 'application/x-www-form-urlencoded';
IdHttp.Post(PostURL,aParams,aStream); //Post
sRst := Utf8ToAnsi(aStream.DataString);
if Pos('FAIL', sRst) <> 1 then
Result := True;
except
on e:exception do
sRst := e.Message;
end;
finally
IdHttp.Free;
aParams.Free;
aStream.Free;
end;
end;
请教一下,TIdHttp这个类,在MFC有没有相同功能的?
或者说我用c++要怎么Post数据过去?
aStream 这个结果又怎么取回来?
谢谢
------解决方案--------------------
refer to WinHttp class, function SendRequest()
------解决方案--------------------
直接上代码
void CBlockInfoLog::INetPost(char* pData, char* pPage, char* pDomain, unsigned int port)
{
int nLen = strlen(pData) + 1024;
char* pHttpHeader = new char[nLen];
memset(pHttpHeader, 0, nLen);
sprintf(pHttpHeader,
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*\r\n"
"Referer: http://%s/\r\n"
"Accept-Language: zh-cn\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"UA-CPU: x86\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; 360SE)\r\n"
"Host: %s\r\n"
"Content-Length: %d\r\n"
"Connection: Keep-Alive\r\n"
"Cache-Control: no-cache",
pDomain, pDomain, strlen(pData));
CInternetSession session(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
CHttpConnection *pConnection;
CHttpFile *pFile;
pConnection = session.GetHttpConnection(pDomain, (INTERNET_PORT)port);
if( !pConnection )
{
return;
}
pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
"/client_post.html");
if( !pFile )
{
return;
}
pFile->AddRequestHeaders(pHttpHeader);
BOOL bRet = pFile->SendRequest(NULL, NULL, pData, strlen(pData));
if(pFile != NULL)
pFile->Close();
if(pConnection != NULL)
pConnection->Close();
session.Close();
SAFE_DELETE(pHttpHeader);
}
INetPost((char*)strLog.c_str(), "client_post.html", "127.0.0.1", CConfig::GetInstance().m_nHttpPort);