获取当前网页的URL [英] Get URL Of Current Web Page

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

问题描述

我正在尝试从(Internet Explorer)中当前网页的地址栏中获取URL.到目前为止,这是我所拥有的,但是只返回一个"E".

I''m trying to grab the URL from the address bar of the current Web Page in (Internet Explorer). This is what I have so far, but it only returns an"E".

int sent=0, recvd=0;
		sent = send(sniff_socket, "GET HTTP/1.0\r\n\r\n", 16, 0);
		char buff99[2048] = {0};
		recvd = recv(sniff_socket,buff99,2048,0);
		cout << "Bytes Sent: " << sent << endl;
		myfile << "Bytes Sent: " << sent << endl;
		cout << "Bytes Received: " << recvd << endl;
		myfile << "Bytes Received: " << recvd << endl;
		cout << "Data Received: " << buff99 << endl;
		myfile << "Data Received: " << buff99 << endl;

推荐答案

好,您需要一种全新的方法.最简单的方法是使用 http://www.codeguru.com/Cpp/IN/ieprogram/article.php/c4403 [ ^ ].这适用于IE 5-10版本(版本10是Windows 8的预发行版本).

此代码是最小控制台程序中该文章的相关部分.

基本思想是附加到Shell Windows枚举器并浏览Windows列表.这是在main()函数中完成的.
然后,它将在PrintBrowserInfo()函数中找到的每个窗口上打印一些详细信息.

Ok, you need a whole new approach. Easiest way would be with shell connectors as described at http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4403[^]. This works with IE versions 5 - 10 (version 10 is pre-release with windows 8).

This code is the relevant parts of that article in a minimal console program.

Basic idea is attach to the shell windows enumerator and go through the list of windows. This is done in the main() function.
It then prints some details on each window found in the PrintBrowserInfo() function.

#include <stdio.h>
#include <wchar.h>
#include <Windows.h>
#include <Exdisp.h>

//This imports the shell extionsions
//we disable warnings of multiple defines
#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>

void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	//These functions return Unicode strings.
	BSTR bstr;
	//Get the window title
	pBrowser->get_LocationName(&bstr);
	wprintf(L"Title: %s\n", bstr);
	SysFreeString(bstr);
	//Detect if this is Windows Explorer (My Computer) or Internet Explorer (the internet)
	IDispatchPtr spDisp;
	char *type = "Windows Explorer";
	if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) {
		MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
		if (spHtmlDocument != NULL) {
			MSHTML::IHTMLElementPtr spHtmlElement;
			spHtmlDocument->get_body(&spHtmlElement);
			if (spHtmlElement != NULL) {
				type = "Internet Explorer";
			}
		}
		spDisp.Release();
	}
	printf(" Type: %s\n", type);
	//Get the URL of the folder or web page
	pBrowser->get_LocationURL(&bstr);
	wprintf(L"  URL: %s\n\n", bstr);
	SysFreeString(bstr);
}

int main() {
	CoInitialize(NULL); //We need COM
	SHDocVw::IShellWindowsPtr spSHWinds;
	IDispatchPtr spDisp;
	//Find all explorer (Windows and Internet) and list them
	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) {
				//spBrowser->AddRef();
				PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr());
				spBrowser.Release();
			}
		}
	} else {
		puts("Shell windows failed to initialise");
	}
	system("PAUSE");
	return 0;
}


自动激活Active Windows资源管理器或Internet Explorer窗口 [

您缺少请求的URI部分.格式为
You are missing the URI part of the request. The format is
METHOD URI VERSION


主URI是/.

例如,如果我将GET HTTP/1.0发送到www.google.com,则会出现404 not found错误.
如果我发送GET / HTTP/1.0,我会得到302 Found(重定向),因为我在澳大利亚,它会将我重定向到www.google.com.au

我为此使用了telnet,ncat(
nmap [


the home URI is /.

For example if I send GET HTTP/1.0 to www.google.com I get a 404 not found error.
If I send GET / HTTP/1.0 I get 302 Found (redirect) because I am in Australia, and it redirects me to www.google.com.au

I was using telnet for this, ncat (part of nmap[^]) works just as well tho.

This however will not get you the address of the web page. It will return the headers and page contents.


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

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