void CMyRegDlg::OnBnClickedButton1()//get cookie
{
// TODO: 在此添加控件通知处理程序代码
//构建登陆表单数据
char *action="/d/upload/";
char *headers="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\n"
"Accept-Language: zh-cn\r\n"
//"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"
"Host: 192.168.0.21\r\n"
"Connection: Keep-Alive\r\n\r\n";
CString szHeaders(headers);
// szHeaders=headers;//.Format (headers,nReadLength,sid);
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,action,NULL,1,NULL,NULL,INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->SendRequest(szHeaders,NULL,NULL);
szHeaders.ReleaseBuffer ();
CString header("");//获取登陆结果中header
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,header,0);
pFile->Close();
delete pFile;
int i=header.Find ("Set-Cookie: ");
if(i>=0)
{
i+=12;
cookie1_value=header.Mid ( i,header.Find (";",i)-i);
int j=header.Find ("Set-Cookie: ",i);
if(j>=0)
{
j+=12;
cookie2_value=header.Mid (j,header.Find (";",j)-j);
}
}
header.ReleaseBuffer ();
UpdateData(FALSE);//更新
}
void CMyRegDlg::OnBnClickedButton2()//get formhash
{
// TODO: 在此添加控件通知处理程序代码
int nReadLength =0;
//构建登陆表单数据
char *action="/d/upload/register.php?infloat=yes&handlekey=register&inajax=1&ajaxtarget=fwin_content_register";
char *headers="Accept: */*\r\n"
"Accept-Language: zh-cn\r\n"
"Referer: http://127.0.0.1/d/upload/\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"
"Host: 127.0.0.1\r\n"
"Connection: Keep-Alive\r\n"
"Cookie: %s\r\n\r\n";
CString szHeaders;
szHeaders.Format (headers,cookie1_value,cookie2_value);
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,action,NULL,1,NULL,NULL,INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->SendRequest(szHeaders,NULL,NULL);
szHeaders.ReleaseBuffer ();
CString data("");//获取登陆结果页面
char szBuff[1025];
while(1)
{
nReadLength = pFile->Read(szBuff, 1024);
if(nReadLength==0) break;
szBuff[nReadLength]='\0';
data=data+szBuff;
}
pFile->Close();
delete pFile;
int i=data.Find ("formhash");
if(i>=0)
{
i+=17;
formhash_value=data.Mid ( i,data.Find ("\"",i)-i);
UpdateData(NULL);
}
data.ReleaseBuffer ();
}
void CMyRegDlg::OnBnClickedButton3()//go to register
{
// TODO: 在此添加控件通知处理程序代码
//构建登陆表单数据
char *action="/d/upload//register.php?regsubmit=yes&inajax=1";
char *headers="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\n"
"Referer: http://127.0.0.1/d/upload/index.php\r\n"
"Accept-Language: zh-cn\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
// "Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"
"Host: 127.0.0.1\r\n"
"Content-Length: %d\r\n"
"Connection: Keep-Alive\r\n"
"Cache-Control: no-cache\r\n"
"Cookie: %s; %s\r\n";
char *form1="formhash=%s";
char *form2="&referer=http%3A%2F%2F127.0.0.1%2Fd%2Fupload%2findex.php&handlekey=register&activationauth=&username=test2&password=test&password2=test&email=test2@mail.com®submit=%CC%E1%BD%BB";
CString formdata;
formdata.Format (form1,formhash_value);
formdata.Append (form2);
int nReadLength=formdata.GetLength ();
CString szHeaders;
szHeaders.Format (headers,nReadLength,cookie1_value,cookie2_value);
CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,action,NULL,1,NULL,NULL,INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_AUTO_REDIRECT);
pFile->SendRequest(szHeaders,(LPVOID)formdata.GetBuffer (),nReadLength);
szHeaders.ReleaseBuffer ();
formdata.ReleaseBuffer ();
CString data("");//获取登陆结果页面
char szBuff[1025];
while(1)
{
nReadLength = pFile->Read(szBuff, 1024);
if(nReadLength==0) break;
szBuff[nReadLength]='\0';
data=data+szBuff;
}
pFile->Close();
delete pFile;
int i=data.Find ("CDATA[");
// CDATA[该用户名已经被注册]
if(i>=0)
{
i+=6;
AfxMessageBox(data.Mid (i,data.Find ("]",i)-i));
}
//if(data.Find ("该用户名已经被注册")) AfxMessageBox("已经存在!");
m_data.SetWindowTextA (data);
data.ReleaseBuffer ();
}
CInternetSession * session;//全局
const int nTimeOut = 3000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3); //重试次数
//以上是session的准备
pConnection=session.GetHttpConnection(server,port);//请求
pConnection->Close ();
delete pConnection;
pConnection=NULL;//请求结束后关闭Connection,但不关闭session
此时,可以进行更多请求:
pConnection=session.GetHttpConnection(server,port);//请求
pConnection->Close ();
delete pConnection;
pConnection=NULL;//请求结束后关闭Connection,但不关闭session
当所有请求结束后
session->Close();
期间不需要如上面那样自己分析cookie数据了,因为session会自动保存和转发。