注册get cookie get formhash go to register

点击打开链接

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会自动保存和转发。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值