Best ways of parsing a URL using C?

后端 未结 10 1811
死守一世寂寞
死守一世寂寞 2020-11-27 15:36

I have a URL like this:

http://192.168.0.1:8080/servlet/rece

I want to parse the URL to get the values:

IP: 192.168.0.1
Por         


        
10条回答
  •  执念已碎
    2020-11-27 15:47

    I wrote a simple code using sscanf, which can parse very basic URLs.

    #include 
    
    int main(void)
    {
        const char text[] = "http://192.168.0.2:8888/servlet/rece";
        char ip[100];
        int port = 80;
        char page[100];
        sscanf(text, "http://%99[^:]:%99d/%99[^\n]", ip, &port, page);
        printf("ip = \"%s\"\n", ip);
        printf("port = \"%d\"\n", port);
        printf("page = \"%s\"\n", page);
        return 0;
    }
    
    ./urlparse
    ip = "192.168.0.2"
    port = "8888"
    page = "servlet/rece"
    

提交回复
热议问题