• C++ POST方式访问网页


    bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &strDescript)//第一个参数为URL头
    {                         //第二个参数为要post表单的内容
        try{                       //第三个参数用于保存页面返回的信息
                             //第四个参数用于记录日志
            strDescript = "提交成功完成!";
            bool bRet = false;
            CString strServer, strObject, strHeader, strRet;
            unsigned short nPort;
            DWORD dwServiceType;
            if(!AfxParseURL www.90168.org (strUrl, dwServiceType, strServer, strObject, nPort))
            {
                strDescript = strUrl + "不是有效有网络地址!";
                return false;
            }
            CInternetSession sess;//Create session

            CHttpFile* pFile;
            //////////////////////////////////////////////
            CHttpConnection *pServer = sess.GetHttpConnection(strServer, nPort);
            if(pServer == NULL)
            {
                strDescript = "对不起,连接服务器失败!";
                return false;
            }
            pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strObject,NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
            if(pFile == NULL)
            {
                strDescript = "找不到网络地址" + strUrl;
                return false;
            }

            pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
            pFile -> AddRequestHeaders("Accept: */*");
            pFile -> SendRequest(NULL, 0, (LPTSTR)(LPCTSTR)strPara, strPara.GetLength());

            CString strSentence;
            DWORD dwStatus;
            DWORD dwBuffLen = sizeof(dwStatus);
            BOOL bSuccess = pFile->QueryInfo(
                HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
                &dwStatus, &dwBuffLen);

            if( bSuccess && dwStatus>=  200 && dwStatus<300)
            {
                char buffer[256];
                memset(buffer, 0, 256);
                int nReadCount = 0;
                while((nReadCount = pFile->Read(buffer, 2048)) > 0)
                {
                    strContent += buffer;
                    memset(buffer, 0, 256);
                }
                bRet = true;
            }
            else
            {
                strDescript = "网站服务器错误" + strUrl;
                bRet = false;
            }
            ////////////////////////////////////////
            pFile->Close();
            sess.Close();
            return bRet;
        }
        catch(...)
        {
            int nCode = GetLastError();
            strDescript.Format("向服务器post失败!错误号:%d", nCode);
            return false;
        }
    }

  • 相关阅读:
    VS中注释的使用
    VS2010中:error C2471: 无法更新程序数据库
    VS2010 MFC中在对话框上添加工具栏以及工具栏提示信息并改变图标支持256色
    CToolBar与CToolBarCtrl以及CStatusBar 与CStatusBarCtrl的区别
    error C2664: “wcscpy”: 不能将参数 1 从“LPSTR”转换为“wchar_t *”
    VS2010 MFC中的Picture控件显示图像
    VS2010 MFC中屏蔽ESC和ENTER键关闭对话框的方法
    MFC中CImageList(图形列表控件)、CTreeCtrl(树形列表控件)的简单用法
    窗口类、窗口类对象与窗口 三者之间关系——孙鑫<VC++深入详解>
    VS2010 MFC中改变static字体颜色、大小、背景颜色(自定义类),及手动关联变量的方法
  • 原文地址:https://www.cnblogs.com/tianshifu/p/6379715.html
Copyright © 2020-2023  润新知