我需要将 URL 拆分为主机、端口和资源。我搜索了很多参考资料,但找不到任何可以帮助我的东西。这就是我想要的样子:
例如: 网址是 - 1.2.3.4:5678/path1/path2.html 必要的输出是:主机 - 1.2.3.4,端口 - 5678,资源 - /path1/path2.html
我就是这么累:
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
int main()
{
string url="http://qwert.mjgug.ouhnbg:5678/path1/path2.html";
size_t found = url.find_first_of("://");
cout<<found<<endl;
string protocol=url.substr(0,found);
size_t found1 =url.find_first_of(":");
cout<<found1<<endl;
string host =url.substr(found+3,found1-found+1);
size_t found2 = url.find_first_of(":/");
string port1 =url.substr(found1+7,found2+found1-1);
string port =url.substr(found2+1);
cout<<protocol<<endl;
cout<<host<<endl;
cout<<port1<<endl;
cout<<port;
return 0;
}
我的预期结果是:
Protocol - http
Host - qwert.mjgug.ouhnbg
Port - 5678
Resource - path1/path2.html
但是我的结果是:
http:
qwert.mj
t.mjgug
//qwert.mjgug.ouhnbg:5678/path1/path2.html
我应该改变什么?
使用 string.first_find_of(":") 获取任意字符第一次出现的索引,使用 string.substr(pos,len) 获取从索引 pos 开始且 length=len 的子字符串;
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
int main()
{
string url="1.2.3.4:5678/path1/path2.html";
size_t found = url.find_first_of(":");
string host=url.substr(0,found);
size_t found1 =url.find_first_of("/");
string port =url.substr(found+1,found1-found-1);
string resource =url.substr(found1);
cout<<host<<endl;
cout<<port<<endl;
cout<<resource;
return 0;
}
url 中带有 http 或 https
int main()
{
string url="http://qwert.mjgug.ouhnbg:5678/path1/path2.html";
size_t found = url.find_first_of(":");
string protocol=url.substr(0,found);
string url_new=url.substr(found+3); //url_new is the url excluding the http part
size_t found1 =url_new.find_first_of(":");
string host =url_new.substr(0,found1);
size_t found2 = url_new.find_first_of("/");
string port =url_new.substr(found1+1,found2-found1-1);
string path =url_new.substr(found2);
cout<<protocol<<endl;
cout<<host<<endl;
cout<<port<<endl;
cout<<path;
return 0;
}
将两者放在一起:
string url = "http://qwert.mjgug.ouhnbg:5678/path1/path2.html";
size_t found = 0;
string protocol;
if (url.rfind("http", 0) == 0) {
// URL starts with http[s]
found = url.find_first_of(":");
protocol = url.substr(0, found);
found += 3; // Step over colon and slashes
}
size_t found1 = url.find_first_of(":", found);
string host;
string port;
string path;
if (string::npos != found1) {
// Port found
host = url.substr(found, found1 - found);
size_t found2 = url.find_first_of("/", found1);
port = url.substr(found1 + 1, found2 - found1 - 1);
path = url.substr(found2);
} else {
// No port
found1 = url.find_first_of("/", found);
if (string::npos == found1) {
// No additional slashes
found1 = url.length();
}
host = url.substr(found, found1 - found);
path = url.substr(found1);
}
cout << "protocol = [" << protocol << "]";
cout << "host = [" << host << "]";
cout << "port = [" << port << "]";
cout << "path = [" << path << "]";