// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <string>

typedef enum
{
    TYPE_NULL,  //XParseURL分析出错,或者地址错误
    TYPE_FTP,
    TYPE_HTTP,
    TYPE_HTTPS,
}URLTYPE;

URLTYPE XParseURL(LPCTSTR lpURL)
{
    int nPort = 0;

    TCHAR lpszHost[1024] = L"0";
    TCHAR lpszObject[1024] = L"0";
    URLTYPE urltype;
    int     nSize = 0;
    int     i;
    TCHAR   lpszURL[1024];
    LPCTSTR lpHttp = _T("http://");
    LPCTSTR lpHttps = _T("https://");
    LPCTSTR lpftp = _T("ftp://");

    lstrcpy(lpszURL, lpURL);

    for (i = 0; i < wcslen(lpszURL); i++)
    {
        if (_T('\\') == lpszURL[i]) lpszURL[i] = _T('/');
    }

    //判断协议
    if (CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, lpHttp, lstrlen(lpHttp), lpURL, lstrlen(lpHttp)))
    {
        nSize = lstrlen(lpHttp);
        urltype = TYPE_HTTP;
    }
    else if (CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, lpHttps, lstrlen(lpHttps), lpURL, lstrlen(lpHttps)))
    {
        nSize = lstrlen(lpHttps);
        urltype = TYPE_HTTPS;

    }
    else if (CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, lpftp, lstrlen(lpftp), lpURL, lstrlen(lpftp)))
    {
        nSize = lstrlen(lpftp);
        urltype = TYPE_FTP;
    }
    else
    {
        urltype = TYPE_NULL;
        return urltype;
    }

    //找页面
    for (i = nSize; i < lstrlen(lpszURL); i++)
    {
        if (_T('/') == lpszURL[i])
        {
            //这里有点小问题
            //if (!lpszObject)
            //{
                lstrcpy((LPWSTR)lpszObject, &lpszURL[i + 1]);
            //};
            lpszURL[i] = 0;
            break;
        }
    }

    //找端口
    for (i = nSize; i < lstrlen(lpszURL); i++)
    {
        if (_T(':') == lpszURL[i])
        {
            if (!nPort)
            {
                nPort = _ttoi(&lpszURL[i + 1]);
            };
            lpszURL[i] = 0;
            break;
        }
    }
    if (lpszHost)
    {
        lstrcpy(lpszHost, &lpszURL[nSize]);
    }

    printf("主机:%ls ,端口:%d ,页面:%ls \n", lpszHost, nPort, lpszObject);

    return urltype;
}

int main()
{
    int i = XParseURL(L"https://www.baidu.com:3389/test/admin.html");

    printf("协议 ");
    //协议判断
    switch (i)
    {
    case 0: printf("NULL \n"); break;
    case 1: printf("ftp \n");  break;
    case 2: printf("http \n");  break;
    case 3: printf("https\n");  break;

    default:

        break;
    }

    return 0;
}

参考

[技术专题] [原创]获取URL中主机域名的小技巧!
http://bbs.pediy.com/thread-179681.htm

【C++】获取URL中主机域名的更多相关文章

  1. Java获取URL中的顶级域名domain的工具类

    方式一: import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import jav ...

  2. js获取URL中的参数

    js获取URL中的一些参数的意思 location对象 含有当前URL的信息. 属性 href 整个URL字符串. protocol 含有URL第一部分的字符串,如http: host 包含有URL中 ...

  3. 获取url中的参数\+发送ajax请求根路径|+获取复选框的值

    //获取url中的参数function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=( ...

  4. 关于获取URL中传值的解决方法

    在我们页面的URL中包含着很多信息,包括域名,协议等等这里就不一一介绍了),对于我们开发者而言,使用比较多的就是页面之间的传值.为什么要页面传值呢?很简单,当你在浏览一个商品页面的时候你要看到一个商品 ...

  5. 使用JavaScript获取url中的参数值

    今天需要用到从url中获取参数,在网上找了几个JavaScript方法,mark下来.   一些可以使用的去获取url中指定的部分:如http://www.mystuff.com.cn/aboutus ...

  6. JS获取url中query_str JavaScript RegExp 正则表达式基础详谈

    面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window.location.href(设置或获取整个 URL ...

  7. 获取URL中的某段字符

    1. Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 window 对象的一部分,可通过 window.Location 属性对其进行访问. ️ ...

  8. 如何获取url中的参数并传递给iframe中的报表

    在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...

  9. 记录一次bug解决过程:velocity中获取url中的参数

    一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...

随机推荐

  1. BZOJ 193题纪念

  2. display position 和float的作用和关系

    1.传统布局由这三者构成. 2.position设为absolute,那么display一定是block,因此对于span来说,就可以设置高和宽了. 3.position为relative ,那么fl ...

  3. A1056. Mice and Rice

    Mice and Rice is the name of a programming contest in which each programmer must write a piece of co ...

  4. java -server 和 -client 的不同,及 java -server 时抛错原因

    在dos窗口运行:java -server Error: missing `server' JVM at `C:\Program Files\Java\jre7\bin\server\jvm.dll' ...

  5. 搭建一个简单的node.js服务器

    第一步:安装node.js.可以去官网:https://nodejs.org/en/进行下载. 查看是否成功,只需在控制台输入 node -v.出现版本号的话,就证明成功了. 第二步:编写node.j ...

  6. css实现单选效果,看看有趣的tabIndex

    以前我实现单选变色几乎都是用js实现的,今天看到有个css属性可以直接实现单选变色,很开心啊~ 话不多说看效果 实现的代码如下 下面我们看看用focus实现别的有趣的效果 话不多说看效果 实现的代码如 ...

  7. MFC中运行出现问题“不支持尝试执行的操作”

    http://blog.csdn.net/maturn/article/details/8051987 问题描述: 基于CDialogEx的对话框工程.VS2010开发环境. 调试运行到OnInitD ...

  8. Failed to load because no supported source was found

    Uncaught (in promise) DOMException: Failed to load because no supported source was found? 等待解决:

  9. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  10. python写GUI

    图形用户界面 本文利用wxpython wx包中的方法都是以大写的字幕开头 import wx def load(event): file = open(filename.GetValue()) co ...