我正在尝试使用WinINet库中的InternetCrackUrl函数解析url。你知道吗
这个函数的“return”值是通过lpUrlComponents参数完成的,该参数有一个defined structure。你知道吗
我现在的问题是,我的结构化类只检索数值字段DWORD
和INT
中的值,但是所有字母数字字段/所有指针LPCWSTR
都是空的(输出在代码块的末尾)。你知道吗
我知道有问题的参数应该是指向变量的指针。我认为那是错误的部分。
我做错什么了?你知道吗
编辑:我在代码块末尾添加了使其正常工作所必需的几行代码。这已经在Win8.1和XP上进行了测试。你知道吗
from ctypes import *
from ctypes.wintypes import *
dll = windll.wininet
url = LPCWSTR("http://user:password@www.host.com:8080/url-path?key=value")
url_length = DWORD(len(url.value))
flags = DWORD()
class URL_COMPONENTS(Structure):
_fields_ = [
("dwStructSize", DWORD),
("lpszScheme", LPCWSTR),
("dwSchemeLength", DWORD),
("nScheme", INT),
("lpszHostName", LPCWSTR),
("dwHostNameLength", DWORD),
("nPort", INT),
("lpszUserName", LPCWSTR),
("dwUserNameLength", DWORD),
("lpszPassword", LPCWSTR),
("dwPasswordLength", DWORD),
("lpszUrlPath", LPCWSTR),
("dwUrlPathLength", DWORD),
("lpszExtraInfo", LPCWSTR),
("dwExtraInfoLength", DWORD),
]
url = LPCWSTR("http://user:password@www.host.com:8080/url-path?key=value")
url_length = DWORD(len(url.value))
flags = DWORD()
url_components = URL_COMPONENTS()
dll.InternetCrackUrlW.restype = c_bool
print "Output of initial question:"
print dll.InternetCrackUrlW(url, url_length, flags, byref(url_components))
for field in url_components._fields_:
print field[0], getattr(url_components, field[0])
print "\nOutput of working:"
# Give the those lengths a nonzero value. == 0 do nothing, != 0 do something
url_components.dwHostNameLength = DWORD(-1)
dll.InternetCrackUrlW(url, url_length, flags, byref(url_components))
# Now we got the string cut off at the start of the desired element.
print "lpszHostName:", url_components.lpszHostName
# And the length of the content.
print "dwHostNameLength:", url_components.dwHostNameLength
# Just cut it out and you get the desired result.
print "HostName:", url_components.lpszHostName[:url_components.dwHostNameLength]
Output of initial question output:
True
dwStructSize 60
lpszScheme None
dwSchemeLength 0
nScheme 3
lpszHostName None
dwHostNameLength 0
nPort 8080
lpszUserName None
dwUserNameLength 0
lpszPassword None
dwPasswordLength 0
lpszUrlPath None
dwUrlPathLength 0
lpszExtraInfo None
dwExtraInfoLength 0
Output of working:
lpszHostName: www.host.com:8080/url-path?key=value
dwHostNameLength: 12
HostName: www.host.com
下面是
URL_COMPONENTS
结构的实现,它将所有字符串缓冲区设置为固定大小,默认为512个字符。你知道吗输出:
根据^{} :
因此,在调用
InternetCrackUrl
之前,为所有感兴趣的项设置长度组件。你知道吗当函数返回时,将为有效组件设置指针,但它们指向您自己的字符串!使用指针和长度从结构中检索它们。你知道吗
相关问题 更多 >
编程相关推荐