winnet编程时 HttpSendRequest调用GetLastError 返回2(系统找不到指定文件)

引用:https://blog.csdn.net/heque/article/details/88540462

在开发一个项目,通过用户名和密码登录访问一个https网站时,模拟浏览器网页登录访问模式,执行httpsendrequest失败,GetLastError 返回2(系统找不到指定文件)

代码:

 

int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
    CString strServer;
    CString strObject;
    DWORD dwServiceType;
    INTERNET_PORT nPort;
    strResponse = "";
 
    AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
 
    if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
    {
        return FAILURE;
    }
    
    //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_RELOAD;
    
    //m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 30);
    //m_pSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
    //m_pSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
    try
    {
        m_pConnection = m_pSession->GetHttpConnection(strServer,
            dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
            nPort);
         
        m_pFile = m_pConnection->OpenRequest(strMethod, strObject, 
            NULL, 1, NULL, NULL, 
            (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
          
          //m_pFile = m_pConnection->OpenRequest(strMethod, strObject, 
    //        NULL, 1, NULL, NULL, 
    //        dwOpenRequestFlags);
 
#if 0
        DWORD dwFlags;
        m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
        dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
        set web server option
        m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
#endif
 
        m_pFile->AddRequestHeaders("Host: sms.huhutv.com.cn");
        m_pFile->AddRequestHeaders("Accept: text/html, */*; q=0.01");
        m_pFile->AddRequestHeaders("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
        m_pFile->AddRequestHeaders("Referer: https://sms.huhutv.com.cn/rtcrm-clientweb/npage/obim/staff/loginmng/initLogin.do");
        m_pFile->AddRequestHeaders("Accept-Language: zh-CN,zh;q=0.8");
        m_pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
        m_pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate, sdch");
        if(strCookie != ""){
            CString str;
            str.Format("Cookie:%s",strCookie);
            m_pFile->AddRequestHeaders(str);
        }else
        {
            m_pFile->AddRequestHeaders("Cookie: JSESSIONID=6F5AA3FA40C60178D776152742886227; insert_cookie=49078854");
        }
        m_pFile->AddRequestHeaders("Connection: keep-alive");
        //MessageBox(NULL,strCookie.GetString(), "strCookie", MB_OK|MB_ICONINFORMATION);
        //ShowFileHeaders();
        m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
 
        char szChars[BUFFER_SIZE + 1] = {0};
        string strRawResponse = "";
        UINT nReaded = 0;
        GetURLCookie();
        
        DWORD dwStatus = 0;
        DWORD dwStatusLen = sizeof(dwStatus);  
        m_pFile->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE,  
                    &dwStatus, &dwStatusLen, 0);
 
        while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
        {
            szChars[nReaded] = '\0';
            strRawResponse += szChars;
            memset(szChars, 0, BUFFER_SIZE + 1);
        }
        
        int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
        WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
        memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
 
        MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
        CString cs(pUnicode);
        delete []pUnicode; 
        pUnicode = NULL;
 
        strResponse = cs;
 
        //Clear();
    }
    catch (CInternetException* e)
    {
        //Clear();
        DWORD dwErrorCode = e->m_dwError;
        e->Delete();
 
        DWORD dwError = GetLastError();
        CString strErr;
        strErr.Format("dwErrorCode = %d", dwErrorCode);
        //LOG::out0("dwError = %d", dwError, 0);
        MessageBox(NULL,strErr.GetString(), "提示", MB_OK|MB_ICONINFORMATION);
        if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
        {
            return OUTTIME;
        }
        else
        {
            return FAILURE;
        }
    }
    return SUCCESS;
}
分析:

1.通过firefox或360浏览器访问该https链接能够正常登录,说明网络和服务器等都没有问题;

2.将代码拷贝到另外一台电脑上运行,执行httpsendrequest没有报错,说明应该是系统存在问题;

经过查找分析后,终于解决;

解决方法:
打开internet explorer, 选择“文件”下拉菜单(没看到的话,按ALT键可以显示菜单栏),然后取消选中“脱机工作”,返回“联机”工作。之后,使用wininet的相关编程将正常工作;SendRequest的返回将正常。

 

另外,internet explorer的脱机工作,还会导致vpn工具的登录失败问题,导致vpn的服务端网络等问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpSendRequest 函数是 Windows API 中的一个函数,可以用于发送 HTTP 请求。以下是一个简单的示例: ```c++ #include <iostream> #include <windows.h> #include <wininet.h> #pragma comment(lib, "wininet.lib") int main() { HINTERNET hSession = InternetOpen("MyApp", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); HINTERNET hConnect = InternetConnect(hSession, "www.example.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); HINTERNET hRequest = HttpOpenRequest(hConnect, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1); char szHeaders[] = "Content-Type: text/html\r\nMySpecialHader: Whatever\r\n"; char szData[1024] = { 0 }; DWORD dwDataLen = 1024; BOOL bResult = HttpSendRequest(hRequest, szHeaders, strlen(szHeaders), szData, dwDataLen); if (bResult) { std::cout << "HTTP request sent successfully" << std::endl; } else { std::cout << "Error sending HTTP request: " << GetLastError() << std::endl; } InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hSession); return 0; } ``` 在这个示例中,我们首先调用 `InternetOpen` 函数创建一个会话句柄,然后使用 `InternetConnect` 函数连接到指定的服务器。接下来,我们使用 `HttpOpenRequest` 函数创建一个 HTTP 请求句柄并指定请求的方法、URL 和其他的请求头信息。在这个示例中,我们只设置了 Content-Type 和一个自定义的头信息 MySpecialHader。然后,我们使用 `HttpSendRequest` 函数发送请求,如果成功则输出一条消息,否则输出错误代码。最后,我们关闭所有的句柄。 需要注意的是,这个示例只发送了一个 GET 请求,如果需要发送 POST 请求或者带有请求体的请求,需要在 `HttpSendRequest` 函数的参数中提供相应的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值