1.Post中文,在vc++中需要urlencode吗(我用了urlencode "post字符" 也不行)?
2.COOKIE值好像不需要手动添加,CHttpConnection似乎已经记录了(好像在使用Ie一样),是这样吗?
在输入错误的验证码后,返回结果提示"输入验证码错误";
输入正确的验证码后,返回提示"没有填写必要的表单";(这个说名中文的验证吗已经发出去了呀)Post文字:
Validatecode=一子饶迷地吵大年&Age=27&NickName=vyciht&Passwd=ojmamafs&Country=中华人民共和国&CityNo=广州&Province=广东&Sex=女&RegisterType=1&public=0字段是正确的,我用vc#写的可以注册完成,vc#中需要urlencodevoid CQQDlg::OnBnClickedReg()
{
BOOL bresult=TRUE;

char szpcode[16+1];
string code;
GetDlgItemText(IDC_CODE,szpcode,16+1);
code=szpcode;
if(code.length()<16)
{
MessageBox("验证码输入错误!");
return;
}
SetUserInfo(code);

//STEP_1 用GET方式获取COOKIE_1
CInternetSession pcis;

CHttpConnection* pHttpconn=pcis.GetHttpConnection(REGSERVER.c_str());
CHttpFile* pHttpfile=pHttpconn->OpenRequest("GET",REGSTEP1URL.c_str());
string header="Referer: HTTP://"+REGSERVER+REGSTEP1URL;
pHttpfile->AddRequestHeaders(header.c_str());
pHttpfile->SendRequest();


pHttpfile->Close();
pHttpconn->Close();
delete pHttpfile;
delete pHttpconn;

//STEP_2 用POST 方式发送数据

CString strPost;


strPost.Append("Validatecode=");
strPost.Append(UserInfo.ValidateCode.c_str()); strPost.Append("&Age=");
strPost.Append(IntToCString(UserInfo.Age)); strPost.Append("&NickName=");
strPost.Append(UserInfo.NickName.c_str()); strPost.Append("&Passwd=");
strPost.Append(UserInfo.UserPass.c_str()); strPost.Append("&Country=");
strPost.Append(UserInfo.Country.c_str()); strPost.Append("&CityNo=");
strPost.Append(UserInfo.City.c_str()); strPost.Append("&Province=");
strPost.Append(UserInfo.Province.c_str()); strPost.Append("&Sex=");
strPost.Append(UserInfo.Sex.c_str()); strPost.Append("&RegisterType=1&public=0"); TRACE(strPost+"\r\n");
CHttpConnection*  pHttpconn1=pcis.GetHttpConnection(REGSERVER_1.c_str());
CHttpFile* pHttpfile1=pHttpconn1->OpenRequest("POST",REGSTEP2URL.c_str());

//pHttpfile1->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded\r\n");//这句使用和不使用都返回一样的结果,加Content-Length也是一样的结果
pHttpfile1->SendRequestEx(strPost.GetLength());
pHttpfile1->WriteString(strPost);
pHttpfile1->EndRequest();
CString l;
while(pHttpfile1->ReadString(l))
{
TRACE(l+"\n"); //page_fail2.htm 验证码错误
//
}
pHttpfile1->Close();
pHttpconn1->Close();


pcis.Close();
delete pHttpfile1;
delete pHttpconn1;
}void CQQDlg::OnBnClickedRe()
{
GetCodeImg();
SetDlgItemText(IDC_CODE,"");
}void CQQDlg::SetUserInfo(string code)
{

srand((unsigned)time(NULL));
//NickName;
UserInfo.NickName="";
for(int i=0;i<6;i++)
{
UserInfo.NickName+=ARRCHAR[GetRandomNum(25)];
}
//UserPass;
UserInfo.UserPass="";
for(int i=0;i<8;i++)
{
UserInfo.UserPass+=ARRCHAR[GetRandomNum(25)];
}

UserInfo.Age=GetRandomNum(20)+20;
UserInfo.Country="中华人民共和国";
UserInfo.City="广州";
UserInfo.Province="广东";
UserInfo.Sex=(GetRandomNum(2)>1)?"男":"女";
UserInfo.ValidateCode=code;}int CQQDlg::GetRandomNum(int max)
{

return (int)(max*(double)rand()/(double)RAND_MAX);}CString CQQDlg::UrlEncode(string str)
{
DWORD len,maxlen;
len=100;
maxlen=100;
char buf[100];
BOOL bresult=AtlEscapeUrl(str.c_str(),buf,&len,maxlen);
ASSERT(bresult); CString stren(buf,len-1);//-1 是去掉缓冲区中的0
return stren;}

解决方案 »

  1.   

    1、http协议中传输的文字都要encode吧
    2、需要InternetSetCookie做这种程序的原则就是完全模拟浏览器,浏览器发什么样的数据给服务器,你就发什么样的数据给服务器。可以使用EffeTech Http Sniffer查看分析浏览器与服务器的通信。
      

  2.   

    自己搞出来了!
    我的理解是这样:
    1.GET获取如有参数,需要编码,POST中文不需要编码,我的代码是这样写的,可以注册了,和VC#不太一样,可能是SendRequestEx(),内部做了编码,(不是很清楚)
    2.COOKIE如我参测的一样,使用CHttpConnection会记录WEB返回的COOKIE,和自动发出去,需要添加额外的COOKIE需要手动添加!