糖果派对官方网站_可以赌钱的糖果游戏_手机版
使用MFC的对wininet封闭的类CHttpConnection及相关类,进行http访问,在某此电脑上访问失败无法与服务器建立连接,地址通

使用MFC的对wininet封闭的类CHttpConnection及相关类,进行http访问,在某此电脑上访问失败无法与服务器建立连接,地址通

作者:网络编程    来源:未知    发布时间:2019-12-24 21:35    浏览量:

代码如下BOOLCHttpTransfer::Post(LPCTSTRubiconszRemoteU福睿斯I,constchar*sendBuf,intsendLen,ReceiveDatarevData,constCStringstrContentType/*=ContentType_TEXT_PLAN*/){assert(NULL!=szRemoteURI);BOOLbResult=FALSE;DWORDdwType=0;CStringsServer=_T("");CStringsObject=_T("");INTERNET_PORTwPort=0;DWORDdwPostSize=0;CHttpConnection*pHttpConn=NULL;CHttpFile*pHttpFile=NULL;CInternetSessioncis(_T(""));bResult=FALSE;try{bResult=AfxParseURL(szRemoteURI,dwType,sServer,sObject,wPort);if(!bResult)returnFALSE;cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5000);cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);pHttpConn=cis.GetHttpConnection(sServer,wPort,NULL,NULL);assert(NULL!=pHttpConn);pHttpFile=pHttpConn-OpenRequest(CHttpConnection::HTTP_VERB_POST,sObject);assert(NULL!=pHttpFile);//准备HTTP头部CStringstrTemp;CStringstrHeaders=_T("Content-Type:")+strContentType;//application/x-+=_T("rnAccept-Language:zh-cn");strHeaders+=_T("rnAccept-Charset:utf-8");//strHeaders+=_T("rnReferer:");//strHeaders+=szRemoteURI;/*设置央浼相关参数*///pHttpFile-AddRequestHeaders(L"Accept:*/*,application/json"卡塔尔国;//accept央浼报头域,表示客商端选择什么项目标音讯//pHttpFile-AddRequestHeaders(L"Accept-Charset:UTF8"卡塔尔(英语:State of Qatar);//pHttpFile-AddRequestHeaders(L"Accept-Language:zh-cn;q=0.8,en;q=0.6,ja;q=0.4"卡塔尔(قطر‎;//pHttpFile-AddRequestHeaders(L"Content-Type:"卡塔尔国+strContentType;//application/x-+=_T("rnAccept-Language:zh-cn");strHeaders+=_T("rnAccept-Charset:utf-8");bResult=FALSE;if(sendLen==0)bResult=pHttpFile-SendRequest(strHeaders,_T("1"卡塔尔(英语:State of Qatar),1卡塔尔;//为了确定保障重回的数额是科学的。elsebResult=pHttpFile-SendRequest(strHeaders,(LPVOID卡塔尔sendBuf,sendLen卡塔尔;if(bResult卡塔尔(英语:State of Qatar)bResult=HttpReceive(pHttpFile,revData卡塔尔(قطر‎;}catch(CFileException*e){e-Delete();//REPORT_CACHED_EXCEPTION(_T("CFileException"));}catch(CInternetException*e){CStringsError;sError.Format(_T("Inernetconnectionerror:%d"),e-m_dwError卡塔尔(قطر‎;//用于重回网络非常音讯TCHA奥迪Q3szError[64]={0};e-GetErrorMessage(szError,64);revData.errInfo=szError;revData.nErrorCode=e-m_dwError;e-Delete();}if(pHttpFile){pHttpFile-Close();deletepHttpFile;}if(pHttpConn){pHttpConn-Close();deletepHttpConn;}cis.Close();returnbResult;}

前面向来通过CHttpClient类的ExecuteRequest函数来调用webapi相关接口,以Post格局传参也基本都以三个参数为主。最近因项目需求Post四个参数(三个是string类型,另叁个是json类型卡塔尔给服务端,爱莫能助了,伏乞大神付与指导,先谢过!CHttpClient类如下://头文件classCHttpClient{public:CHttpClient(LPCTSTHavalstrAgent=IE_bb电子糖果派对,AGENT);virtual~CHttpClient(void);intHttpGet(LPCTSTRstrUrl,LPCTSTRstrPostData,stringstrResponse);intHttpPost(LPCTSTRstrUrl,LPCTSTRstrPostData,stringstrResponse);private:intExecuteRequest(LPCTSTRstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,stringstrResponse);voidClear();private:CInternetSession*m_pSession;CHttpConnection*m_pConnection;CHttpFile*m_pFile;};//源文件intCHttpClient::ExecuteRequest(LPCTSTRstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,stringstrResponse){CStringstrServer;CStringstrObject;DWORDdwServiceType;INTERNET_PORTnPort;strResponse="";char*psText;AfxParseURL(strUrl,dwServiceType,strServer,strObject,nPort);if(AFX_INET_SERVICE_HTTP!=dwServiceTypeAFX_INET_SERVICE_HTTPS!=dwServiceType){returnFAILURE;}try{m_pConnection=m_pSession-GetHttpConnection(strServer,dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_CONNECT:SECURE_CONNECT,nPort,_T(""),_T(""));m_pFile=m_pConnection-OpenRequest(strMethod,strObject,NULL,1,NULL,NULL,(dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_REQUEST:SECURE_REQUEST));DWORDdwFlags;m_pFile-QueryOption(INTERNET_OPTION_SECURITY_FLAGS,dwFlags);dwFlags|=SECURITY_FLAG_IGNORE_UNKNOWN_CA;//setwebserveroptionm_pFile-SetOption(INTERNET_OPTION_SECURITY_FLAGS,dwFlags);m_pFile-SetOption(INTERNET_OPTION_SEND_TIMEOUT,PDLG-m_stAPP.nHttpSendTimeOut);m_pFile-SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT,PDLG-m_stAPP.nHttpSendTimeOut);m_pFile-SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT,PDLG-m_stAPP.nHttpSendTimeOut);m_pFile-SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,PDLG-m_stAPP.nHttpSendTimeOut);m_pFile-AddRequestHeaders(_T("Accept:*,*/*"));m_pFile-AddRequestHeaders(_T("Accept-Language:zh-cn"));m_pFile-AddRequestHeaders(_T("Content-Type:application/json"));m_pFile-AddRequestHeaders(_T("Accept-Encoding:gzip,deflate"));DWORDdwNum=WideCharToMultiByte(CP_UTF8,NULL,strPostData,-1,NULL,0,NULL,FALSE);psText=newchar[dwNum];if(!psText){delete[]psText;returnFAILURE;}WideCharToMultiByte(CP_UTF8,NULL,strPostData,-1,psText,dwNum,NULL,FALSE);//m_pFile-SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPostData,strPostData==NULL?0:_tcslen(strPostData));m_pFile-SendRequest(NULL,0,(LPVOID)psText,strPostData==NULL?0:dwNum);delete[]psText;charszChars[BUFFER_SIZE+1]={0};charbuf[BUFFER_SIZE];memset(buf,0,BUFFER_SIZE);UINTnLen=0;stringstrRawResponse="";UINTnReaded=0;while((nReaded=m_pFile-Read((void*)szChars,BUFFER_SIZE))0){szChars[nReaded]='';strRawResponse+=szChars;memcpy(buf[nLen],szChars,nReaded);nLen+=nReaded;memset(szChars,0,BUFFER_SIZE+1);}intunicodeLen=MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0);WCHAR*pUnicode=newWCHAR[unicodeLen+1];memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,pUnicode,unicodeLen);CStringcs(pUnicode);delete[]pUnicode;pUnicode=NULL;//strResponse=cs;USES_CONVERSION;stringstr(W2A(cs));strResponse=str;Clear();}returnSUCCESS;}

#define_WIN32_WINNT0400//定义全局变量意义?#defineSTRICT#defineWIN32_LEAN_AND_MEAN#defineWH_KEYBOARD_LL13#defineWH_MOUSE_LL14#includeiostream#includefstream#includestring#includecmath#includeafxinet.h//定义了MFC的CInternetSession类等#includewinsock2.h#includewindows.h#pragmacomment(lib,"ws2_32")//#pragmacomment(linker,"/subsystem:"windows"/entry:"mainCRTStartup""卡塔尔(قطر‎usingnamespacestd;//文件名全局变量charfilename[42]={0};//原始数据构造体structrawDATA{inttime;stringinfo;intx;inty;intkey;doubled;}rdata[3];//这些构造体生龙活虎共有多少个成员,以数组的款式累积变量名称为rdata/**post函数*参数:全局变量hostName的引用pathnamepostData*/boolPostHttpPage(conststd::stringhostName,conststd::stringpathName,conststd::stringpostData卡塔尔//传入的是函数的引用并不是函数的参数本人{CInternetSessionsession("sjsz"卡塔尔;//创设一个Internet会话//session.SetOption(INTERAV4NET_OPTION_CONNECT_TIMEOUT,3000卡塔尔;//3秒的连年超时//session.SetOption(INTELacrosseNET_OPTION_SEND_TIMEOUT,3000卡塔尔;//3秒的出殡和下葬超时//session.SetOption(INTEENVISIONNET_OPTION_RECEIVE_TIMEOUT,3000卡塔尔国;//3秒的摄取超时//session.SetOption(INTEWranglerNET_OPTION_DATA_SEND_TIMEOUT,3000卡塔尔(قطر‎;//3秒的出殡和下葬超时//session.SetOption(INTE奥迪Q5NET_OPTION_DATA_RECEIVE_TIMEOUT,3000卡塔尔;//3秒的选用超时//session.SetOption(INTETucsonNET_OPTION_CONNECT_RETRIES,1);//1次重试try{INTERNET_PORTnPort=80;DWORDdwRet=0;/**c_str()函数是为了保障c++字符串与C包容而转用为c中的字符串指针的花样*/CHttpConnection*pServer=session.GetHttpConnection(hostName.c_str(),nPort);if(!pServer){returnfalse;}CHttpFile*pFile=pServer-OpenRequest(CHttpConnection::HTTP_VERB_POST,pathName.c_str());if(!pFile卡塔尔(قطر‎{returnfalse;}CStringstrHeaders="Content-Type:application/x-";//央求头//起首发送央浼pFile-SendRequest(strHeaders,(LPVOID卡塔尔(英语:State of Qatar)postData.c_str(),postData.size());pFile-QueryInfoStatusCode(dwRet);if(dwRet==HTTP_STATUS_OK卡塔尔{CStringresult,newline;while(pFile-ReadString(newline卡塔尔(英语:State of Qatar)卡塔尔(英语:State of Qatar){//循环读取每行内容result+=newline;}//决断是或不是有重新设置命令,_T转换为8位if(result==_T("reset")){rdata[2].x=0;rdata[2].y=0;rdata[2].key=0;rdata[2].d=0;}if(result==_T("exit"卡塔尔卡塔尔(قطر‎{exit(0卡塔尔(قطر‎;}std::coutresultstd::endl;//突显重返内容//printf("%s",result卡塔尔(قطر‎;}else{returnfalse;}deletepFile;deletepServer;}catch(CInternetException*pEx){//catcherrorsfromWinInetTCHARpszError[200];pEx-GetErrorMessage(pszError,200卡塔尔(قطر‎;std::coutpszErrorstd::endl;//显示至极消息returnfalse;}session.Close(卡塔尔;//关闭sessionreturntrue;}//数据深入解析发送analyzevoidpostdata(卡塔尔国{//[0]本次数据,[1]上次数量,[2]总和数据//,[3]平平均数量据if(rdata[1].x){if(rdata[0].info=="lup")rdata[2].x++;elseif(rdata[0].info=="rup")rdata[2].y++;elseif(rdata[0].info=="keyup")rdata[2].key++;elseif(rdata[0].info=="syskeyup")rdata[2].key++;elseif(rdata[0].info=="move")rdata[2].d=rdata[2].d+sqrt(pow((rdata[0].y-rdata[1].y),2)+pow((rdata[0].x-rdata[1].x卡塔尔,2卡塔尔(قطر‎卡塔尔(英语:State of Qatar)/1000;}//解析截至,此次数据存入[1]中rdata[1]=rdata[0];}//键盘钩子布局体typedefstructtagKBDLLHOOKSTRUCT{DWO君越DvkCode;DWOTiggoDscanCode;DWOWranglerDflags;DWOTiguanDtime;DWOPAJERODdwExtraInfo;}KBDLLHOOKSTRUCT,FAHaval*LPKBDLLHOOKSTRUCT,*PKBDLLHOOKSTRUCT;//鼠标钩子布局体typedefstructtagMSLLHOOKSTRUCT{POINTpt;DWO中华VDmouseData;DWO宝马X5Dflags;DWO陆风X8Dtime;DWOPanameraDdwExtraInfo;}MSLLHOOKSTRUCT,FA奇骏*LPMSLLHOOKSTRUCT,*PMSLLHOOKSTRUCT;DWORDg_main_tid=0;HHOOKg_kb_hook=0;HHOOKg_ms_hook=0;BOOLCALLBACKcon_handler(DWORD){PostThreadMessage(g_main_tid,WM_QUIT,0,0卡塔尔国;returnTRUE;};//键盘回调函数LRESULTCALLBACKkb_proc(intcode,WPARAMw,LPARAMl){PKBDLLHOOKSTRUCTp=(PKBDLLHOOKSTRUCT)l;constchar*info=NULL;if(w==WM_KEYDOWN)info="keydn";elseif(w==WM_KEYUP)info="keyup";elseif(w==WM_SYSKEYDOWN)info="syskeydn";elseif(w==WM_SYSKEYUP卡塔尔国info="syskeyup";//写入文件SYSTEMTIMEsys;GetLocalTime(sys卡塔尔;//ofstreamoutfile(filename,ios::app卡塔尔(قطر‎;//outfile(sys.wHour*10000000+sys.wMinute*100000+sys.wSecond*1000+sys.wMilliseconds)//","info","p-vkCode","p-scanCodeendl;//outfile.close();rdata[0].time=sys.wHour*10000000+sys.wMinute*100000+sys.wSecond*1000+sys.wMilliseconds;rdata[0].info=info;rdata[0].key=p-scanCode;postdata();printf("%s-vkCode[%04x],scanCode[%04x]n",info,p-vkCode,p-scanCode);Sleep(1);returnCallNextHookEx(g_kb_hook,code,w,l卡塔尔;};//鼠标回调函数LRESULTCALLBACKms_proc(intcode,WPARAMw,LPARAMl){PMSLLHOOKSTRUCTp=(PMSLLHOOKSTRUCT)l;constchar*info=NULL;switch(w){caseWM_LBUTTONDOWN:info="ldn";break;caseWM_LBUTTONUP:info="lup";break;caseWM_LBUTTONDBLCLK:info="lclick";break;caseWM_RBUTTONDOWN:info="rdn";break;caseWM_RBUTTONUP:info="rup";break;caseWM_RBUTTONDBLCLK:info="rclick";break;//caseWM_MBUTTONDOWN:info="m";break;//caseWM_MBUTTONUP:info="mup";break;////caseWM_MBUTTONDBLCLK://button=MouseButtons.Middle;//break;default:info="move";break;}//写入文件SYSTEMTIMEsys;GetLocal提姆e(sys卡塔尔(英语:State of Qatar);//ofstreamoutfile(filename,ios::app卡塔尔;//outfile(sys.wHour*10000000+sys.wMinute*100000+sys.wSecond*1000+sys.wMilliseconds)//","info","p-pt.x","p-pt.yendl;//outfile.close();rdata[0].time=sys.wHour*10000000+sys.wMinute*100000+sys.wSecond*1000+sys.wMilliseconds;rdata[0].info=info;rdata[0].x=p-pt.x;rdata[0].y=p-pt.y;postdata();printf("%s-x[%d],y[%d]n",info,p-pt.x,p-pt.y);Sleep(1000);returnCallNextHookEx(g_ms_hook,code,w,l);};//创设文件voidcreatfile(卡塔尔(英语:State of Qatar){cout"createfilemethodstart--2"endl;//获取Computer名WSADATAwsData;WSAStartup(MAKEWOXC90D(1,1卡塔尔国,wsData卡塔尔国;charszHostName[32]="";gethostname(szHostName,sizeof(szHostName卡塔尔(قطر‎卡塔尔(英语:State of Qatar);WSACleanup(卡塔尔;//获取系统时间SYSTEMTIMEsys;GetLocalTime(sys卡塔尔(قطر‎;//总结文件名sprintf(filename,"%d",(sys.wYear*10000+sys.wMonth*100+sys.wDay卡塔尔卡塔尔;strcat(filename,"+"卡塔尔;strcat(filename,szHostName卡塔尔(قطر‎;strcat(filename,".txt"卡塔尔(英语:State of Qatar);coutfilenameendl;cout"Q1未有开创文件--3"endl;//创制文件//ofstreamoutfile(filename卡塔尔;//outfile.close(卡塔尔(英语:State of Qatar);}DWOHighlanderDWINAPIFun1Proc(LPVOIDlpParameter卡塔尔国;//threaddata//主程序intmain(卡塔尔{std::cout"programstart--1"std::endl;//增添互斥确定保证程序唯生机勃勃HANDLEhMutex=::CreateMutex(NULL,TRUE,"szsj"卡塔尔(英语:State of Qatar);//互斥锁,第三个参数是指向广安的指针,第1个参数书开端化互斥对象的持有者,第两个参数是指向互斥对象名的指针//到如今截至,只是创立了三个名称为szsj的锁,可是,未有锁任何事物if(GetLastError(卡塔尔==E奥迪Q5RO普拉多_ALREADY_EXISTS卡塔尔(قطر‎//检查错误代码,假使有同名的次序行使财富,则会回来错误代码EKoleosRORubicon_ALREADY_EXISTS{cout"已启动!"endl;exit(0卡塔尔国;}if(hMutex卡塔尔(英语:State of Qatar){::ReleaseMutex(hMutex卡塔尔(قطر‎;//解锁}creatfile(卡塔尔;//创立文件cout"getname"filenameendl;HANDLEhThread1;//创设线程hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL卡塔尔(قطر‎;CloseHandle(hThread1卡塔尔国;g_main_tid=GetCurrentThreadId();SetConsoleCtrlHandler(con_handler,TRUE卡塔尔国;//安装键盘钩子g_kb_hook=SetWindowsHookEx(//键盘钩子能够赢得键盘活动的连锁新闻WH_KEYBOARD_LL,kb_proc,GetModuleHandle(NULL卡塔尔(英语:State of Qatar),// 不能够为NULL,不然失利0卡塔尔;if(g_kb_hook==NULL){fprintf(stderr,"SetWindowsHookExfailedwitherror%dn",::GetLastError(卡塔尔(英语:State of Qatar)卡塔尔国;return0;};//安装鼠标钩子g_ms_hook=SetWindowsHookEx(WH_MOUSE_LL,ms_proc,GetModuleHandle(NULL卡塔尔(قطر‎,// 不能为NULL,否则失利0卡塔尔(英语:State of Qatar);if(g_ms_hook==NULL){fprintf(stderr,"SetWindowsHookExfailedwitherror%dn",::GetLastError(卡塔尔(英语:State of Qatar)卡塔尔;return0;};//新闻循环MSGmsg;while(GetMessage(msg,NULL,0,0卡塔尔(قطر‎卡塔尔{TranslateMessage(msg卡塔尔(英语:State of Qatar);DispatchMessage(msg卡塔尔;};//卸载钩子UnhookWindowsHookEx(g_kb_hook);UnhookWindowsHookEx(g_ms_hook卡塔尔国;return0;}//线程1的入口函数DWOHighlanderDWINAPIFun1Proc(LPVOIDlpParameter卡塔尔//threaddata{while(1卡塔尔{stringname;charpdata[300];//机器名name=filename;size_tiPos1=name.find("+");size_tiPos2=name.find("."卡塔尔国;name=name.substr(iPos1+1,iPos2-iPos1-1);//左键右键开关间距赋值sprintf(pdata,"name=%sleft=%dright=%dkey=%ddistance=%.2f",name.c_str(),rdata[2].x,rdata[2].y,rdata[2].key,rdata[2].d);PostHttpPage("sz.zwill.cn","00.php",pdata);coutpdataendl;Sleep(1000);}return0;}

下一篇:没有了
友情链接: 网站地图
Copyright © 2015-2019 http://www.tk-web.com. bb电子糖果派对有限公司 版权所有