WinHttpReq.SetProxy方法求大佬详解!!
# -*- coding: utf-8 -*-from urllib import parse
from win32com.client import Dispatch
import pythoncom
g_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
def SendWinHttpReq(WinHttpReq, sendUrl, reUrl=False, body=None, reType=1, headDic=None, code='utf-8'):
global g_headers
WinHttpReq.SetOption(6, reUrl)
if body == None:
WinHttpReq.Open('GET', sendUrl, False)
else:
WinHttpReq.Open('POST', sendUrl, False)
WinHttpReq.SetRequestHeader('User-Agent', g_headers['User-Agent'])
if headDic != None:
for i in headDic:
try:
WinHttpReq.SetRequestHeader(i,headDic)
except:
continue
if body == None:
WinHttpReq.Send()
else:
if type(body) != type('11'):
body = parse.urlencode(body)
WinHttpReq.Send(body)
WinHttpReq.WaitForResponse()
if reType==1:
try:
objstr = WinHttpReq.ResponseBody.obj
except:
return ''
if objstr.startswith(b'\xef\xbb\xbf'):
objstr = objstr[3:]
if code!=None:
return objstr.decode(code,'ignore')
return objstr
if reType==2:
try:
return WinHttpReq.GetResponseHeader('Location')
except:
try:
objstr = WinHttpReq.ResponseBody.obj
except:
return ''
if objstr.startswith(b'\xef\xbb\xbf'):
objstr = objstr[3:]
if code != None:
return objstr.decode(code, 'ignore')
return objstr
return WinHttpReq.GetAllResponseHeaders()
def getWinHttpReq(typeid, timeOut=25):
pythoncom.CoInitialize()
WinHttpReq = Dispatch('WinHttp.WinHttpRequest.5.1')
WinHttpReq.SetTimeouts(8*10000, 8*10000, timeOut*10000, timeOut*10000)
if typeid == 2:
HTTPREQUEST_PROXYSETTING_PROXY = 2
WinHttpReq.SetProxy(HTTPREQUEST_PROXYSETTING_PROXY,
"x.x.x.x:xx",
"*.microsoft.com")# 请问这个*.microsoft.com有什么用,我传""也是可以的
return WinHttpReq
if __name__ == '__main__':
winhttp = getWinHttpReq(2)
headData = {
'Content-Type': 'application/x-www-form-urlencoded',
}
postUrl = 'https://www.pubu.com.tw/'
authUrl = SendWinHttpReq(winhttp, postUrl, True, None, 1, headData)
cookie = winhttp.GetAllResponseHeaders()
print(cookie) https://docs.microsoft.com/zh-cn/windows/win32/winhttp/iwinhttprequest-setproxy?redirectedfrom=MSDN
这个api文档里面不是有写吗?你直接*是全部都匹配 wifengqingyang 发表于 2021-7-28 16:25
https://docs.microsoft.com/zh-cn/windows/win32/winhttp/iwinhttprequest-setproxy?redirectedfrom=MSDN
...
问题是我不写. microsoft.com。代{过}{滤}理设置也有效啊。所以就好奇为啥。
页:
[1]