AW_somedaysomeone 2014年01月14日
HTTPS 的 POST问题
我这几天模仿浏览器对谷歌进行了数据post,但是我用wfetch工具进行post的时候,结果是正常的,过程如下:
Host:accounts.google.com\r\n
Method:POST\r\n
Path:/ServiceLoginAuth\r\n
Scheme:https\r\n
Version:HTTP/1.1\r\n
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n
Accept-Encoding:gzip,deflate,sdch\r\n
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4\r\n
Allow-Chrome-signin:1\r\n
Cache-Control:max-age=0\r\n
Content-Length:1539\r\n
Content-Type:application/x-www-form-urlencoded\r\n
Cookie:GoogleAccountsLocale_session=en; GAPS=XXXXXXXXXXXXX; GALX=XXXXXXXXXXXXX\r\n
Origin:https://accounts.google.com\r\n
Referer:https://accounts.google.com/ServiceLogin?..........\r\n
User-Agent:Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36\r\n
X-Chrome-Variations:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n
\r\n
<Content>\r\n
返回的结果是正确的,返回的是一个跳转页面:
HTTP/1.1 302 Moved Temporarily\r\n
.......
Location: https://accounts.youtube.com/accounts/SetSID?ssdc=1.......\r\n
的结果。
但是,我用wininet进行https的post时,却返回的是200 OK的结果,仍旧停留在原来的页面下,服务器对我的POST没有做出反应 ,但是代码里
我加的头部信息和在Wfetch工具下是一模一样的。请各位大神指点迷津。


BOOL HttpClient::GetOtherCookies(){
//初始化WININET
LPCTSTR lpszAgent = "accounts.google.com";
HINTERNET hinternet = InternetOpenA(lpszAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); //WININET初始化
if(!hinternet){
printf("WININET Initial Failed!\n");
return false;
}
//初始化连接参数并测试是否可连接
LPCTSTR servername = "accounts.google.com";
INTERNET_PORT port = INTERNET_DEFAULT_HTTPS_PORT; //设置为https协议通信端口
LPCTSTR usernames = NULL;
LPCTSTR passwords = NULL;
DWORD dconnetflag = 0;
DWORD dwconnctcontent = 0;

HINTERNET hconnect = InternetConnectA(hinternet,servername,port,usernames,passwords,INTERNET_SERVICE_HTTP,dconnetflag,dwconnctcontent);
if(!hconnect){
printf("Connect Initial Failed!\n");
return false;
}
//创建请求
LPCTSTR lpVerb = "POST";
LPCTSTR lppath = "ServiceLoginAuth";
LPCTSTR lpszVersion = "HTTP/1.1"; // 默认.
LPCTSTR *lplpszAcceptTypes = NULL; // Accpet所有类型.
DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
//设置启用HTTPS
INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID|
INTERNET_FLAG_RELOAD;
DWORD dwOpenRequestContext = 0;
HINTERNET hrequest = ::HttpOpenRequestA(hconnect,lpVerb,lppath,lpszVersion,NULL,lplpszAcceptTypes,dwOpenRequestFlags,dwOpenRequestContext);
if(!hrequest){
return false;
}
//初始化https头部信息

TCHAR HostName[] = _T("Host:accounts.google.com\r\n"); //Host
TCHAR Method[] = _T("Method: POST\r\n"); //Method
TCHAR Path[] = _T("Path: /ServiceLoginAuth\r\n");//Path
TCHAR Schame[] = _T("Scheme: https\r\n"); //Schame
TCHAR Version[] = _T("Version: HTTP/1.1\r\n"); //Version
TCHAR Accept[] = _T("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"); //Accept
TCHAR AcceptEncoding[] = _T("Accept-Encoding: gzip,deflate,sdch\r\n"); //Accept-Encoding
TCHAR AcceptLanguage[] = _T("Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4\r\n");//Accept-Language
TCHAR AllowChromeSignin[] = _T("Allow-Chrome-Signin: 1\r\n"); //Allow-Chrome-Signin
TCHAR CacheControl[] = _T("Cache-Control: max-age=0\r\n"); //Cache-Control
TCHAR ContentLengnth[] = _T("Content-Length: 1539\r\n");
TCHAR ContentType[] = _T("Content-Type: application/x-www-form-urlencoded\r\n");
TCHAR Cookies[] = _T("Cookie: GoogleAccountsLocale_session=en; GAPS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; GALX=XXXXXXXXXXXXXXXXXXXXX\r\n"); //Cookie
TCHAR Origin[] = _T("Origin: https://accounts.google.com\r\n");
TCHAR Referer[] = _T("Referer: https://accounts.google.com/ServiceLogin?service=lso&passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?response_type%3Dcode%26scope%3Demail%2Bprofile%26redirect_uri%3Durn:ietf:wg:oauth:2.0:oob%26client_id%3D567762248606.apps.googleusercontent.com%26hl%3Den-US%26from_login%3D1%26as%3D455dd26da5c7a7ab<mpl=embedded&shdf=Co0DCxIRdGhpcmRQYXJ0eUxvZ29Vcmwa_QEvL2ltYWdlcy1sc28tb3BlbnNvY2lhbC5nb29nbGV1c2VyY29udGVudC5jb20vZ2FkZ2V0cy9wcm94eT91cmw9aHR0cHM6Ly9lbmNyeXB0ZWQtdGJuMi5nc3RhdGljLmNvbS9pbWFnZXM_cSUzRHRibjpBTmQ5R2NTdVQ2b1hYSmNBNVVRWDZLUmdFQjBJN05FTDRQc2gxX2xuV1l5Y2pZVl9FcG1LRkhDOHl3JmNvbnRhaW5lcj1sc28mZ2FkZ2V0PWEmcmV3cml0ZU1pbWU9aW1hZ2UvKiZyZXNpemVfaD0xMjAmcmVzaXplX3c9MTIwJm5vX2V4cGFuZD0xDAsSFXRoaXJkUGFydHlEaXNwbGF5TmFtZRoSc3VwZXJIZXJvVlNtb25zdGVyDAsSBmRvbWFpbhoSc3VwZXJIZXJvVlNtb25zdGVyDAsSFXRoaXJkUGFydHlEaXNwbGF5VHlwZRoSTkFUSVZFX0FQUExJQ0FUSU9ODBIDbHNvIhTlKb_Tc3hnq7mLmSGhPoJ6sAYHSygBMhTVsMBXqA_-AlhGCHJVHryr7gCCbg&sarp=1&scc=1\r\n");
TCHAR UserAgent[] = _T("User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 X-Chrome-Variations:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n"); //User-Agent
TCHAR XChromeVariations[] = _T("X-Chrome-Variations: CIC2yQEIirbJAQiltskBCKm2yQEIwbbJAQiehsoBCIWIygEIlorKAQ==\r\n");
TCHAR NewLine[] = _T("\r\n");

char szPostData[] = "GALX=zDN1s-...................";//将要挂载的内容
int sizes = strlen(szPostData);
printf("%d",sizes);
int reqt;
reqt = ::HttpAddRequestHeadersA(hrequest,HostName,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Host
reqt = ::HttpAddRequestHeadersA(hrequest,Method,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Method
reqt = ::HttpAddRequestHeadersA(hrequest,Path,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Path
reqt = ::HttpAddRequestHeadersA(hrequest,Schame,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Schame
reqt = ::HttpAddRequestHeadersA(hrequest,Version,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Version
reqt = ::HttpAddRequestHeadersA(hrequest,Accept,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Accept
reqt = ::HttpAddRequestHeadersA(hrequest,AcceptEncoding,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Accept-Encoding
reqt = ::HttpAddRequestHeadersA(hrequest,AcceptLanguage,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Accept-Language
reqt = ::HttpAddRequestHeadersA(hrequest,AllowChromeSignin,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Allow-Chrome-Signin
reqt = ::HttpAddRequestHeadersA(hrequest,CacheControl,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //Cache-Control
reqt = ::HttpAddRequestHeadersA(hrequest,ContentLengnth,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
reqt = ::HttpAddRequestHeadersA(hrequest,ContentType,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
reqt = ::HttpAddRequestHeadersA(hrequest,Cookies,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
reqt = ::HttpAddRequestHeadersA(hrequest,Origin,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //
reqt = ::HttpAddRequestHeadersA(hrequest,Referer,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //
reqt = ::HttpAddRequestHeadersA(hrequest,UserAgent,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //User-Agent
reqt = ::HttpAddRequestHeadersA(hrequest,XChromeVariations,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
reqt = ::HttpAddRequestHeadersA(hrequest,NewLine,-1,HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE); //NewLine

BOOL SentRet;
SentRet = ::HttpSendRequestA(hrequest, NULL, 0, szPostData, strlen(szPostData));
if(!SentRet&&::GetLastError()== ERROR_INTERNET_INVALID_CA){
cout<<"请求错误!\n"<<endl; //发送未成功或者证书无效
return false;
}
DWORD size = 1024;
char ReplayHeader[1024]={0}; //接收返回头部的缓存
BOOL qi;
qi =HttpQueryInfo(hrequest,HTTP_QUERY_RAW_HEADERS_CRLF,ReplayHeader,&size,0);
string result = ReplayHeader;
BOOL ContRet=true;

DWORD dwBytesRead=0;
string tmp;
string wholetmp;

while(ContRet){
char content[1024];
ContRet = InternetReadFile(hrequest,content,1024,&dwBytesRead);

printf(content);
printf("\n");
}
//获取Cookie值
printf("<--------------------------GET COOKIE VALUE------------------------------>\n");

printf("<------------------------------------------------------------------------>\n");
return true;
};
...全文
148 点赞 收藏 2
写回复
2 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告