在 .NET 中的 createDocumentFromUrl 调用中包含 post 变量
我有一个 Visual Basic 2010 Express 项目,它使用 IHTMLDocument 对象来解析网页。这是我用来检索网页的函数:
Private Function GetHTML(ByVal url As String)
Dim htmldoc As New HTMLDocument()
Dim ihtml2 As IHTMLDocument2
Dim ihtml3 As IHTMLDocument3
Dim iPersistStream As IPersistStreamInit
iPersistStream = DirectCast(htmldoc, IPersistStreamInit)
iPersistStream.InitNew()
ihtml2 = htmldoc.createDocumentFromUrl(url, vbNullString)
Do Until ihtml2.readyState = "complete"
'required for htmlresult.readyState to transition from "loading" to "complete"
System.Windows.Forms.Application.DoEvents()
Loop
ihtml3 = DirectCast(ihtml2, IHTMLDocument3)
Return ihtml3
End Function
我基本上使用该函数做类似的事情:
ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y")
ihtml.getElementsByTagName("A")
ihtml.getElementById("myel")
etc, etc...
我试图弄清楚当我检索 HTML 文档时如何在 URL 字符串之外包含 POST 变量。我的意思是我希望能够做到这样的事情:
ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y",["postvar1=a","postvar2=b"])
所以我想修改我现有的 GetHTML 函数,以便允许我包含 post 变量(如果可能),如果不可能,我想知道是否也许还有另一种方法可以做到这一点。感谢任何可以提供帮助的人。
I have a project in Visual Basic 2010 Express that parses web pages use the IHTMLDocument object. Here is the function I am using to retrieve a web page:
Private Function GetHTML(ByVal url As String)
Dim htmldoc As New HTMLDocument()
Dim ihtml2 As IHTMLDocument2
Dim ihtml3 As IHTMLDocument3
Dim iPersistStream As IPersistStreamInit
iPersistStream = DirectCast(htmldoc, IPersistStreamInit)
iPersistStream.InitNew()
ihtml2 = htmldoc.createDocumentFromUrl(url, vbNullString)
Do Until ihtml2.readyState = "complete"
'required for htmlresult.readyState to transition from "loading" to "complete"
System.Windows.Forms.Application.DoEvents()
Loop
ihtml3 = DirectCast(ihtml2, IHTMLDocument3)
Return ihtml3
End Function
I'm basically doing stuff like this with the function:
ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y")
ihtml.getElementsByTagName("A")
ihtml.getElementById("myel")
etc, etc...
I'm trying to figure out how I can include POST variables in addition to the URL string when I retrieve the HTML document. By which I mean I'd like to be able to something like this:
ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y",["postvar1=a","postvar2=b"])
So I'd like to modify my existing GetHTML function in order to allow me to include post variables if that is possible and if not I'd like to know if there is perhaps another way to do this. Thanks to anyone who can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我会使用 WebCilent 或 WebRequest 但是...如果您愿意对于此设计:
您必须导航到该页面,找到每个输入字段,设置值,然后在该页面的按钮上调用 InvokeMember。 WebBrowser 的重点是自动化 Web 浏览器,而不是以编程方式发出 HTTP 请求。
示例代码:
http://muftisubzero.blogspot.com/2010/12 /playing-with-webbrowser-class-cnet.html
Ok, I would have used WebCilent or WebRequest but... if you're committed to this design:
You have to navigate to the page, find each input field, set the value then call InvokeMember on the button for that page. WebBrowser is all about automating a web browser, not so much about making HTTP requests programatically.
Sample code:
http://muftisubzero.blogspot.com/2010/12/playing-with-webbrowser-class-cnet.html