我试图用c++编写函数来解析URL,并从url中获取端口号和协议,并将其替换为不同的端口号和协议。
为。例如:原始URL
https://abc.com:8140/abc/bcd我需要用http和端口号6143替换https。并合并URL路径,如
http://abc.com:6143/abc/bcd我使用操作系统作为windows 7和VisuladStudio6.0。
谢谢,
发布于 2013-07-31 16:03:26
使用MFC的快速和肮脏的解决方案:
static TCHAR strhttp[] = L"http:" ;
void Replace(CString & oldurl, CString & newurl, LPTSTR newport)
{
int colonposition ;
colonposition = oldurl.Find(':') ;
if (colonposition != -1)
{
newurl = (CString)strhttp + oldurl.Mid(colonposition + 1) ;
colonposition = newurl.Find(':', _tcslen(strhttp) + 1) ;
if (colonposition != -1)
{
int slashposition = newurl.Find('/', colonposition) ;
newurl = newurl.Left(colonposition + 1) + newport + newurl.Mid(slashposition) ;
}
}
}用法:
CString oldurl = L"https://abc.com:8140/abc/bcd";
CString newurl ;
Replace(oldurl, newurl, L"6143") ;
// now newurl contains the transformed URL发布于 2013-07-31 13:01:05
解决问题的一个方法是正则表达式。
发布于 2013-07-31 13:08:03
解析令牌的字符串:http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx
https://stackoverflow.com/questions/17971099
复制相似问题