C++模拟Http/Https访问web站点
#include "afxinet.h"
CInternetSession mysession;
CHttpConnection *myconn;
CString VoidText;
CString strSentence, strGetSentence;
int _my;
CString Call_Url = "www.zc91.cn";
INTERNET_PORT nPort;
DWORD dwServiceType;
CString strServerName;
CString str ;
DWORD _dwStatus, _dwStatusLen;
CString _Url, _UrlHttp = "https://";
_Url.Format("%s%s", _UrlHttp, Call_Url);//这里拼成的字符串是"https://www.zc91.cn/"
BOOL _GetParse = AfxParseURL(_Url, dwServiceType, strServerName, str , nPort);//调用解析URL方法,自动得到端口号
BOOL _session = mysession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20); //设置连接超时20秒
_session = mysession.EnableStatusCallback(TRUE);
myconn = mysession.GetHttpConnection(Call_Url, INTERNET_FLAG_SECURE, nPort, NULL, NULL);//连接https服务器
CHttpFile* myfile = myconn->OpenRequest(CHttpConnection::HTTP_VERB_POST, "/hys.php", NULL, 1,
NULL, NULL, INTERNET_FLAG_SECURE |
INTERNET_FLAG_EXISTING_CONNECT |
INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID/*| INTERNET_FLAG_TRANSFER_ASCII*/);//打开请求 ,这几个标识都要加上
try
{
DWORD dwFlags;
BOOL _query = myfile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS | INTERNET_OPTION_SECURITY_CERTIFICATE, dwFlags); //认证作者无效或者不正确
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA; //查询选择项
_my = myfile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);//设置选择项
CString _Content = "Accept: */*\r\nUser-Agent:Jinhill Http Agent\r\nContent-Type: application/x-www-form-urlencoded";
CString SendStr;
SendStr.Format("action=submitted&mu=%s&type=m3u8", "url");
if (myfile->SendRequest(_Content, (LPVOID)(LPCTSTR)SendStr, SendStr.GetLength()))//发送数据内容
{
myfile->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE,
&_dwStatus, &_dwStatusLen, 0); //这里可以看到是否发送成功 _dwStatus 等于200时成功
//MessageBox("消息传递完成!");
}
while (myfile->ReadString(strSentence))//读取接收到的方法
{
strGetSentence += strSentence;
}
}
catch (CInternetException *e) {
e->ReportError();
//TCHAR szErr[1024];
//if (e->GetErrorMessage(szErr, sizeof(szErr)))
// AfxMessageBox(szErr); //这个看看具体错误
//e->Delete();
}
delete myfile;
myfile = NULL;
delete myconn;
myconn = NULL;