18,356
社区成员
发帖
与我相关
我的任务
分享
CInternetSession session(_T("My Browser2"));
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strAddress = "http://bbs.zynews.com/login.aspx?loginsubmit=true";
try
{
CString strServer;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if(!AfxParseURL(strAddress,dwServiceType,strServer,strObject,nPort)||
dwServiceType != INTERNET_SERVICE_HTTP)
{
MessageBox(_T("不是HTTP站点"),_T("错误"));
}
pServer = session.GetHttpConnection(strServer,nPort);
//===================================登录(打开登陆页面)===============================================
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObject,NULL,1,NULL,NULL, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT );
pFile->AddRequestHeaders(_T("My Browser2"));
pFile->SendRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
//===================================登录(进行登陆操作)===============================================
CString strFormatData = "username=joyswgood&password=ghygood1&question=0&answer=&expires=43200&templateid=0&login=%E7%99%BB%E5%BD%95";//for 中原网
pFile->AddRequestHeaders("Accept-Language: zh-cn\r\n");
pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded\r\n");
pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate\r\n");
BOOL ret = pFile->SendRequest(0,0,(LPVOID)strFormatData.GetBuffer(),strFormatData.GetLength());
if(ret)
{
ret = pFile->QueryInfoStatusCode(dwRet);
TRACE("login dwRet = %d\n",dwRet);
CString a;
FILE *fp = fopen("c:\\login.html","w+");
while(pFile->ReadString(a))
{
fprintf(fp,"%s",a);
}
fclose(fp);
}
pFile->Close();
//===================================发帖(打开发帖页面)===============================================
strAddress = "http://bbs.zynews.com/postreply.aspx?topicid=8717";
if(!AfxParseURL(strAddress,dwServiceType,strServer,strObject,nPort)|| dwServiceType != INTERNET_SERVICE_HTTP)
{
MessageBox(_T("不是HTTP站点"),_T("错误"));
}
pFile = pServer->OpenRequest(0,strObject,NULL,1,NULL,NULL, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT );
pFile->AddRequestHeaders(_T("My Browser"));
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
TRACE("open[%s] = %d\n",strObject,dwRet);
//===================================发帖(进行发帖操作)===============================================
CString key = _T("7d91f3931e8c");
CString strHeadData;
strHeadData.Format(_T("Content-Type: multipart/form-data; boundary=---------------------------%s\r\n"),key);
strFormatData.Format(_T("-----------------------------%s\r\n"
"Content-Disposition: form-data; name=\"usesig\"\r\n\r\n"
"1\r\n"
"-----------------------------%s\r\n"
"Content-Disposition: form-data; name=\"postreplynotice\"\r\n\r\n"
"on\r\n"
"-----------------------------%s\r\n"
"Content-Disposition: form-data; name=\"title\"\r\n\r\n"
"da jia hao\r\n"
"-----------------------------%s\r\n"
"Content-Disposition: form-data; name=\"postid\"\r\n\r\n"
"-1\r\n"
"-----------------------------%s\r\n"
"Content-Disposition: form-data; name=\"message\"\r\n\r\n"
"sshang hai huanying nin\r\n"
"-----------------------------%s--\r\n\r\n\r\n"),
key,key,key,key,key,key);
pFile->AddRequestHeaders(_T("Content-Length: 581"));
pFile->AddRequestHeaders(_T("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, "
"application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"));
pFile->AddRequestHeaders(_T("Accept-Language: zh-cn\r\n"));
pFile->AddRequestHeaders(strHeadData);
pFile->AddRequestHeaders(_T("Referer: http://bbs.zynews.com/showtopic-8717.aspx"));
ret = pFile->SendRequest(NULL,0,(LPVOID)strFormatData.GetBuffer(),strFormatData.GetLength());
if(ret)
{
ret = pFile->QueryInfoStatusCode(dwRet);
TRACE("Post dwRet = %d\n",dwRet);
CString a;
FILE *fp = fopen("c:\\post.html","w+");
if(fp == NULL) return;
while(pFile->ReadString(a))
{
fprintf(fp,"%s",a);
}
fclose(fp);
}
pFile->Close();
session.Close();
}
catch(...)
{
}