CInternetSession::OpenURL

简介: CInternetSession::OpenURL

PSS ID Number: 172551


Article Last Modified on 3/7/2005


The information in this article applies to:


  • The Microsoft Foundation Classes (MFC), when used with:


  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0


  • Microsoft Visual C++, 32-bit Professional Edition 5.0


  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0


  • Microsoft Visual C++, 32-bit Professional Edition 6.0


  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q172551


SYMPTOMS


CInternetSession::OpenURL() fails if you use a URL that specifies a file that contains spaces or other unsafe characters. For example, the following will fail:


CMyInternetSession s("");
CStdioFile* p = s.OpenURL("file://c:/temp/Text Document.txt");

 

As a result, a File Exception is thrown and the following message appears:

c:/temp/Text%20Document.txt was not found.


CAUSE


The specifications for a URL designate certain characters that must be escaped when they are present in a URL. The space character is one of the characters that must be escaped.


The function CInternetSession::OpenURL() calls a function that converts unsafe characters in the URL to their escape sequences before processing the URL further. If the URL is using the file protocol it creates a CStdioFile and returns a pointer to this object. However, CStdioFile does not understand the escaped path and, as a result, an exception is thrown.


RESOLUTION


When you use the file protocol, you must not convert unsafe characters to their escape sequences. The following code demonstrates how to do this by writing a CInternetSession derived class to override OpenURL().


// class definition
   class CMyInternetSession : CInternetSession
   {
   public:
      CMyInternetSession::CMyInternetSession(LPCTSTR pstrAgent = NULL,
         DWORD dwContext = 1, DWORD dwAccessType =
         PRE_CONFIG_INTERNET_ACCESS, LPCTSTR pstrProxyName = NULL,
         LPCTSTR pstrProxyBypass = NULL, DWORD dwFlags = 0);
      CStdioFile* CMyInternetSession::OpenURL(LPCTSTR pstrURL,
         DWORD dwContext = 1, DWORD dwFlags =
         INTERNET_FLAG_TRANSFER_ASCII, LPCTSTR pstrHeaders = NULL,
         DWORD dwHeadersLength = 0);
   };
   // CMyInternetSession constructor
   CMyInternetSession::CMyInternetSession(LPCTSTR pstrAgent,
      DWORD dwContext, DWORD dwAccessType, LPCTSTR pstrProxyName,
      LPCTSTR pstrProxyBypass, DWORD dwFlags)
   {
      CInternetSession(pstrAgent, dwContext, dwAccessType, pstrProxyName,
         pstrProxyBypass, dwFlags);
   }
   CStdioFile* CMyInternetSession::OpenURL(LPCTSTR pstrURL,
      DWORD dwContext, DWORD dwFlags, LPCTSTR pstrHeaders,
      DWORD dwHeadersLength)
   {
      ASSERT(pstrURL != NULL);
      ASSERT(dwHeadersLength == 0 || pstrHeaders != NULL);
      // must have TRANSFER_BINARY or TRANSFER_ASCII but not both
      #define _AFX_TRANSFER_MASK (INTERNET_FLAG_TRANSFER_BINARY / 
         | INTERNET_FLAG_TRANSFER_ASCII)
      ASSERT((dwFlags & _AFX_TRANSFER_MASK) != 0);
      ASSERT((dwFlags & _AFX_TRANSFER_MASK) != _AFX_TRANSFER_MASK);
      DWORD dwServiceType;
      CString strServer;
      CString strObject;
      INTERNET_PORT nPort;
      BOOL bParsed = AfxParseURL(pstrURL, dwServiceType,
         strServer, strObject, nPort);
      // if it turns out to be a file...
      if (bParsed && dwServiceType == AFX_INET_SERVICE_FILE)
      {
         CString userName;
         CString password;
         // Get the normalized file name with out converting
         // unsafe characters to escape sequence.
         AfxParseURLEx(pstrURL, dwServiceType, strServer,
            strObject, nPort, userName, password, ICU_NO_ENCODE);
         int nMode = CFile::modeRead | CFile::shareCompat;
         if (dwFlags & INTERNET_FLAG_TRANSFER_BINARY)
            nMode |= CFile::typeBinary;
         else
            nMode |= CFile::typeText;
         return new CStdioFile(strObject, nMode);
      }
      return CInternetSession::OpenURL(pstrURL,   dwContext, dwFlags,
         pstrHeaders, dwHeadersLength);
   }


相关文章
|
定位技术
百度地图开发:字符串经纬度转为经纬度数组的解决方案
百度地图开发:字符串经纬度转为经纬度数组的解决方案
130 0
|
API Windows
[笔记]Windows核心编程《番外篇》常用的NT API及使用示例
Windows核心编程《番外篇》常用的NT API及使用示例
732 0
[笔记]Windows核心编程《番外篇》常用的NT API及使用示例
|
3天前
|
机器学习/深度学习 人工智能 算法
解密巴黎奥运会中的阿里云AI技术
2024年巴黎奥运会圆满结束,中国代表团金牌数与美国并列第一,展现了卓越实力。阿里云作为官方云服务合作伙伴,通过先进的AI技术深度融入奥运的各项环节,实现了大规模的云上转播,超越传统卫星转播,为全球观众提供流畅、高清的观赛体验。其中,“子弹时间”回放技术在多个场馆的应用,让观众享受到了电影般的多角度精彩瞬间。此外,8K超高清直播、AI智能解说和通义APP等创新,极大地提升了赛事观赏性和互动性。能耗宝(Energy Expert)的部署则助力实现了赛事的可持续发展目标。巴黎奥运会的成功举办标志着体育赛事正式进入AI时代,开启了体育与科技融合的新篇章。
解密巴黎奥运会中的阿里云AI技术
|
10天前
|
开发框架 自然语言处理 API
基于RAG搭建企业级知识库在线问答
本文介绍如何使用搜索开发工作台快速搭建基于RAG开发链路的知识库问答应用。
7592 16
|
17天前
|
弹性计算 关系型数据库 Serverless
函数计算驱动多媒体文件处理:高效、稳定与成本优化实践
本次测评的解决方案《告别资源瓶颈,函数计算驱动多媒体文件处理》展示了如何利用阿里云函数计算高效处理多媒体文件。文档结构清晰、内容详实,适合新客户参考。方案提供了一键部署与手动部署两种方式,前者简便快捷,后者灵活性高但步骤较多。通过部署,用户可体验到基于函数计算的文件处理服务,显著提升处理效率和系统稳定性。此外,测评还对比了应用内处理文件与函数计算处理文件的不同,突出了函数计算在资源管理和成本控制方面的优势。
22674 19
|
11天前
|
SQL 分布式计算 数据库
畅捷通基于Flink的实时数仓落地实践
本文整理自畅捷通总架构师、阿里云MVP专家郑芸老师在 Flink Forward Asia 2023 中闭门会上的分享。
8196 14
畅捷通基于Flink的实时数仓落地实践
|
18天前
|
机器学习/深度学习 存储 人工智能
提升深度学习性能的利器—全面解析PAI-TorchAcc的优化技术与应用场景
在当今深度学习的快速发展中,模型训练和推理的效率变得尤为重要。为了应对计算需求不断增长的挑战,AI加速引擎应运而生。其中,PAI-TorchAcc作为一个新兴的加速引擎,旨在提升PyTorch框架下的计算性能。本文将详细介绍PAI-TorchAcc的基本概念、主要特性,并通过代码实例展示其性能优势。
17693 147
|
11天前
|
前端开发 Java Go
关于智能编码助手【通义灵码】,开发者们这么说...
现在通过体验活动首次完成通义灵码免费下载及使用的新用户,即可获得限量定制帆布包 1 个;分享体验截图到活动页面,还可参与抽奖活动,iPhone15 手机、机械键盘、智能手环等大奖等你拿!
7158 11
|
13天前
|
人工智能 JSON Serverless
【AI 冰封挑战】搭档函数计算,“冰”封你的夏日记忆
夏日炎炎,别让高温打败你的创意,立即体验 ComfyUI 自制冰冻滤镜!无需繁琐的后期技巧,三步开启一段清凉无比的视觉探险。参与实验并上传作品即可获得运动无线蓝牙耳机,限量 800 个,先到先得!
8246 11