Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the code I am running.

C++
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  1 URL: %s\n\n", bstr);
	myfile <<"\n  1 URL: << bstr;
	/////////////////////////////////
	std::wstring wsURL;
	wsURL = bstr;
 	size_t DSlashLoc = wsURL.find(L"//");
 	if (DSlashLoc != wsURL.npos)
	{
		wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 6);
	}
	DSlashLoc = wsURL.find(L"/");
	if (DSlashLoc != wsURL.npos)
		wsURL.erase(DSlashLoc);
 	wprintf(L"\n   2 URL: %s\n\n", wsURL.c_str());
	myfile <<"\n   2 URL: << wsURL.c_str();
	///////////////////////////////////
	SysFreeString(bstr);
}


The probem is this...on the second set of wprint.
if a url is http://www.codeproject.com
I get
codeproject.com which is great.
however if a url is http://stackoverflow.com
I get
tackoverflow.com which is not so great.
How can I fix this?
Thnak you.
Posted
Updated 8-Nov-11 6:27am
v3

1 solution

The issue is that you have DSlashLoc + 6 as the end offset. This will always remove the starting protocol and colon, the // and the next 4 characters.

C++
//First remove the http, https, ftp, ...
size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != wsURL.npos)
{
	wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
}
//Now remove the www. if it exists, otherwise leave it alone
DSlashLoc = wsURL.find(L"www.");
if (DSlashLoc == 0)
{
	wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
}
 
Share this answer
 
Comments
Member 7766180 8-Nov-11 12:36pm    
Thank you Andrew! You are a God!
JackDingler 9-Nov-11 17:16pm    
You may only be interested in the domain name...

But I'd like to point out that www.mydomain.com and mydomain.com can resolve to different IP addresses and different servers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900