获取基本网址 [英] Getting The Basic Web Address

查看:88
本文介绍了获取基本网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从当前浏览器窗口的位置栏中获取地址,例如"http://www.codeproject.com/KB/webforms/FileUploadWithProgrss.aspx"
但是我只想要这个:www.codeproject.com

我该怎么办?

以下是我的代码.

I''m getting the address from the location bar of my current browser window, like this "http://www.codeproject.com/KB/webforms/FileUploadWithProgrss.aspx"
However I just want this: www.codeproject.com

How would I do this?

Below is my code.

#include "stdafx.h"
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include <exdisp.h>

#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>
 
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	IDispatchPtr spDisp;
	if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) {
		MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
		if (spHtmlDocument != NULL) {
			MSHTML::IHTMLElementPtr spHtmlElement;
			spHtmlDocument->get_body(&spHtmlElement);
		}
		spDisp.Release();
	}
	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  URL: %s\n\n", bstr);
	SysFreeString(bstr);
}
 
int main() {
	CoInitialize(NULL);
	SHDocVw::IShellWindowsPtr spSHWinds;
	IDispatchPtr spDisp;
	if (spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK) {
		long nCount = spSHWinds->GetCount();
		for (long i = 0; i < nCount; i++) {
			_variant_t va(i, VT_I4);
			spDisp = spSHWinds->Item(va);
			SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
			if (spBrowser != NULL) {
				PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr());
				spBrowser.Release();
			}
		}
	} else {
		puts("Shell windows failed to initialise");
	}
	system("PAUSE");
	return 0;
}



我正在使用多字节字符集.



I''m using multi-byte character set.

推荐答案

一种方法是使用开源Boost库,请参见 http://stackoverflow.com/questions /2151854/c-resolve-a-host-ip-address-from-a-url [
One way is to use the Open Source boost library, see http://stackoverflow.com/questions/2616011/easy-way-to-parse-a-url-in-c-cross-platform[^].

This is another way: http://stackoverflow.com/questions/2151854/c-resolve-a-host-ip-address-from-a-url[^].

—SA


您可以尝试......

You can try this...

std::wstring wsURL = bstr;

size_t DSlashLoc = wsURL.find(L"//");

if (DSlashLoc >= 0)
{
    wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 2);
    wsURL.erase(wsURL.begin() + wsURL.find(L'/'));
}

wprintf(L"  URL: %s\n\n", wsURL.c_str());


这篇关于获取基本网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆