[python-win32] win32com: HTTP Post with WebBrowser.Navigate()
Troy Farrell
troy at studyshare.net
Fri Aug 26 21:29:37 CEST 2005
I've made some progress. I finally coerced IE to POST, but the data it
posts is ugly binary data, not what I thought I sent in PostData:
---Begin Python Code---
import win32com.client
# Prepare Internet Explorer
ie = win32com.client.Dispatch( "InternetExplorer.Application.1" )
dURL = "http://server/cgi-bin/test.sh"
Flags = 0
Huh = ""
Data = "TEST=SUCCESS"
PostData = [ Data ]
Headers = "Content-Type: application/x-www-form-urlencoded\r\n"
# show it to the user
ie.Visible = True
ie.Navigate( dURL, Flags, Huh, PostData, Headers )
---End Python Code---
Where a the QUERY_STRING would be posted, "TEST=SUCCESS" in the example
above, I'm getting wierd binary data:
POST /cgi-bin/test/sh HTTP/1.1
Accept: */*
Accept-Language: en-us,ru;q=0.8,fr;q=0.5,et;q=0.3
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR
1.1.4322; .NET CLR 2.0.50215)
Host: server
Content-Length: 16
Connection: Keep-Alive
Cache-Control: no-cache
.........}......
The POSTed data formatted in a python array:
[ 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x7d, 0x19, 0x00,
0x00, 0x00, 0x00, 0x00 ]
I suspect this is due to a disagreement between Python and COM over the
PostData argument. In more than one place on support.microsoft.com and
MSDN I have seen this:
"""
The post data specified by PostData is passed as a SAFEARRAY Data Type
structure. The VARIANT should be of type VT_ARRAY and point to a SAFEARRAY
Data Type. The SAFEARRAY Data Type should be of element type VT_UI1,
dimension one, and have an element count equal to the number of bytes of
post data.
"""
This was mentioned long ago on the python list:
http://mail.python.org/pipermail/python-list/2002-July/113260.html
Does anyone know how I might go about fixing this argument? This is where
I mention that I have little/no COM experience outside of python.
Thanks.
Troy
> Hello List!
>
> I am trying to use win32com to automate Internet Explorer. I am able to
> instanciate my COM object, and even control IE, but using
> WebBrowser.Navigate() to execute an HTTP POST request fails; the browser
> always makes a GET request. The MS KB article on doing this with VB is
> here:
> http://support.microsoft.com/default.aspx?kbid=174923
>
> Possibly related bug here:
> http://support.microsoft.com/default.aspx?scid=kb;en-us;322122
>
> I have tried to model my Python after this code, but to no avail:
>
> ---Being Python Code---
>
> from pythoncom import Missing
> import win32com.client
>
> class Events:
> def OnDocumentComplete(self, pDisp=Missing, URL=Missing):
> """Fired when the document being navigated to reaches
> ReadyState_Complete."""
> if URL == "about:blank":
> self.Navigate( dURL, Missing, Missing, PostData, Headers )
>
>
> # Prepare Internet Explorer
> ie = win32com.client.DispatchWithEvents( "InternetExplorer.Application.1",
> Events )
>
> dURL = "http://server/cgi-bin/test.sh"
> Data = "TEST=SUCCESS"
> PostData = list( Data )
> Headers = "Content-Type: application/x-www-form-urlencoded\n\r"
>
> # show it to the user
> ie.Visible = True
> ie.Navigate( "about:blank" )
>
> ---End Python Code---
>
> Every time, the broser makes a GET request to the URL. Can anyone point
> me in the right direction?
>
> Thanks.
> Troy Farrell
More information about the Python-win32
mailing list