CHttpFile 模拟登录京东失败 提示请使用浏览器cookie获请刷新页面后重试

小修仔仔 2014-06-23 08:44:16
void CHttpManager::LoginJD(/*char *szBuff,int & szSize*/)
{

CString strHttpName = "http://passport.jd.com/new/login.aspx";
//CString strHeaders = "Content-Type: application/x-www-form-urlencoded; charset=utf-8"; // 请求头
//CString strFormData = "LoginUserName=wjyoem&LoginPassword=qq123123";

//pConnection = m_Session.GetHttpConnection(strHttpName);
//m_HttpFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,"");
m_http = (CHttpFile *)m_senssion.OpenURL(strHttpName);
CString strSentence, strGetSentence="";
if(m_http)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
//BOOL result = m_HttpFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
BOOL bSuccess = m_http->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
while(m_http->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
}
// 取返回的COOKIE
CString strInfo ;
DWORD dw = 0 ;
while(m_http->QueryInfo(HTTP_QUERY_SET_COOKIE ,strInfo ,&dw))
{
m_strCookie += strInfo ;
}
string returnVal = strGetSentence;

int nStart = returnVal.find("acid=");
if (nStart != returnVal.npos)
{
int nEnd = returnVal.find("&",nStart);
m_strPic = returnVal.substr(nStart+5,nEnd-nStart-5);
}
nStart = returnVal.find("name=\"uuid\"");
if (nStart != returnVal.npos)
{
nStart = returnVal.find("value=\"");
int nEnd = returnVal.find("\"",nStart+7);
m_strUuid = returnVal.substr(nStart+7,nEnd-nStart-7);
}
nStart = returnVal.find("使用合作");
if (nStart != returnVal.npos)
{
nStart = returnVal.find("name=\"",nStart);
int nEnd = returnVal.find("\"",nStart+6);
m_strName = returnVal.substr(nStart+6,nEnd-nStart-6);
}
nStart = returnVal.find("value=\"",nStart);
if (nStart != returnVal.npos)
{
int nEnd = returnVal.find("\"",nStart+7);
m_strValue = returnVal.substr(nStart+7,nEnd-nStart-7);
}

time_t timep;
time (&timep);
long ltime = timep;

string strUrl = "https://authcode.jd.com/verify/image?a=1&acid=";
strUrl += m_strPic;
strUrl += "&uid=";
strUrl += m_strPic;
strUrl += "&yys=";

CString strTime;
strTime.Format("%d",ltime);
strUrl += strTime;

strGetSentence = "";
m_http = (CHttpFile *)m_senssion.OpenURL(strUrl.c_str());
bSuccess = m_http->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);

char szBuff[1024*100];
memset(szBuff, 0, sizeof(szBuff));
DWORD dwFileLen = m_http->GetLength();
DWORD ret;
DWORD dwRead=10;

HANDLE hFile;

hFile=CreateFile("d://1.png",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);


while(dwRead>0)
{
dwRead=m_http->Read(szBuff,sizeof(szBuff)-1);
WriteFile(hFile,szBuff,dwRead,&ret,NULL);
}

CloseHandle(hFile);
}
}

void CHttpManager::LoginJD2(CString strPic)
{
CString strHttpName = "passport.jd.com";
CString strHeaders = "Content-Type: application/x-www-form-urlencoded; charset=utf-8"; // 请求头
CString strFormData = "LoginUserName=wjyoem&LoginPassword=qq123123";


CString strCookie = "Cookie: ";
strCookie += m_strCookie.c_str();
strCookie.Replace("Path:/;","");
strCookie.Replace("HttpOnly","");

m_http->AddRequestHeaders(strCookie);

m_connection = m_senssion.GetHttpConnection(strHttpName);

string strUrl = "/uc/loginService?uuid=";
strUrl += m_strUuid;

SYSTEMTIME st;
GetLocalTime(&st);
double time;
SystemTimeToVariantTime(&st,&time);

time = time/100000;

CStringA str;
str.Format("%.17lf",time);

strUrl += "&&r=";
strUrl += str;
m_http = m_connection->OpenRequest(CHttpConnection::HTTP_VERB_POST,strUrl.c_str(),NULL,1,0,0,INTERNET_FLAG_NO_COOKIES|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_AUTO_REDIRECT);

string strPost = "uuid=";
strPost += m_strUuid;
strPost += "&loginname=a409012486&nloginpwd=25853322&loginpwd=25853322&machineNet=&machineCpu=&machineDisk=&authcode=";
strPost += strPic;
strPost += "&";
strPost += m_strName;
strPost += "=";
strPost += m_strValue;

CString strSentence, strGetSentence="";

if(m_http)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL result = m_http->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
BOOL bSuccess = m_http->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
while(m_http->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
USES_CONVERSION;
string strRet;
Utf8ToMb(strGetSentence.GetBuffer(),strGetSentence.GetLength(),strRet);
}
}

m_http->Close();
delete m_http;

}
...全文
1504 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我也卡在这里了,我加了 pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,m_strObjectName, NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_COOKIES );//带 也还是不行,我获取的cookie总是不完全的,想问lz是怎么获取到完整的cookie的
oyljerry 2014-06-23
  • 打赏
  • 举报
回复
浏览器的cookie没有被你的POST带上
小修仔仔 2014-06-23
  • 打赏
  • 举报
回复
引用 4 楼 VisualEleven 的回复:
运行你的程序抓包,和运行浏览器程序抓包,比对两个包的数据?确定发送的数据一致??
什么工具能抓我程序的包呀,我是根据抓浏览器的包 写到程序里post过去的
Eleven 2014-06-23
  • 打赏
  • 举报
回复
运行你的程序抓包,和运行浏览器程序抓包,比对两个包的数据?确定发送的数据一致??
小修仔仔 2014-06-23
  • 打赏
  • 举报
回复
引用 1 楼 VisualEleven 的回复:
CInternetSession::GetCookie()函数得到该cookie信息 ?? 用抓包工具抓个包,和用浏览器访问的数据比对一下,看看缺少什么信息?
我用httpwatch抓的包~~然后写的代码,应该是什么都不差的呀。
小修仔仔 2014-06-23
  • 打赏
  • 举报
回复
我用httpwatch抓的包~~然后写的代码,应该是什么都不差的呀。
Eleven 2014-06-23
  • 打赏
  • 举报
回复
CInternetSession::GetCookie()函数得到该cookie信息 ?? 用抓包工具抓个包,和用浏览器访问的数据比对一下,看看缺少什么信息?
RLib 2014-06-23
  • 打赏
  • 举报
回复
这里有篇C++模拟登录Blog.com代码http://blog.csdn.net/rrrfff/article/details/8872540,用的是winsock

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧