.Net Winfoms WebBrowser控件-当从Stream加载内容时,如何允许提交操作?
我在 Visual Studio 2010 中的 Windows 窗体应用程序上使用 WebBrowser 控件 - 面向 .Net Framework 3.5。
我已通过设置 DocumentStream 属性加载了 WebBrowser 控件的内容。流内容来自对第三方网页的 Http (POST) 请求的响应,该请求在代码中使用 HttpWebRequest 对象进行调用。我们需要使用 POST 请求动词类型。该表单根据请求参数填充数据。
在 Windows 应用程序中,用户需要填写一些附加文本字段,然后提交。通过流加载后,页面不知道原始页面的完整 url。因此提交失败(在 WebBrowser 控件中显示页面的名称)。
有没有办法为控件提供文档的完整路径,以便提交操作具有正确的上下文?设置 WebBrowser.Url 属性不起作用,因为这只会导致导航到页面而不显示数据,因为它没有传递任何参数。
下面是代码(到目前为止):
//Class to call website to make http post
var webBridge = new WebCallHandler();
//Make the request. Response returned as string
var result = webBridge.MakeHttpRequest();
//Get string as stream
var byteArray = Encoding.ASCII.GetBytes(result);
var stream = new MemoryStream(byteArray) { Position = 0 };
//webBrowser1.Url = new Uri(URL);
webBrowser1.DocumentStream = stream;
//Need to set the context of the page like "http://example.com/somepage.aspx"
var dom = webBrowser1.Document.DomDocument;
I am using the WebBrowser control on a Windows Forms application in Visual Studio 2010 - targeting .Net framework 3.5.
I have loaded the contents of the WebBrowser control via setting the DocumentStream property. The stream content is from the response to an Http (POST) request to a third party web page which is called in code using the HttpWebRequest object. We need to use the POST request verb type. The form is populated with data based on the request parameters.
Within the Windows application, the user needs to fill out a few additional text fields and then submit. Having been loaded via the stream, the page has no knowledge of full url of the original page. Therefore the submit fails (displays the name of the page in the WebBrowser control).
Is there any way to give the control the full path to the document such that the Submit operation will have the correct context? Setting WebBrowser.Url property does not work as this simply results in navigation to the page without the data displayed as it is not passed any parameters.
Below is the code (so far):
//Class to call website to make http post
var webBridge = new WebCallHandler();
//Make the request. Response returned as string
var result = webBridge.MakeHttpRequest();
//Get string as stream
var byteArray = Encoding.ASCII.GetBytes(result);
var stream = new MemoryStream(byteArray) { Position = 0 };
//webBrowser1.Url = new Uri(URL);
webBrowser1.DocumentStream = stream;
//Need to set the context of the page like "http://example.com/somepage.aspx"
var dom = webBrowser1.Document.DomDocument;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您已填充
result
变量(似乎是一个字符串),您应该能够轻松修改内容。使用例如 正则表达式 或 HTML Agility Pack 搜索
并替换它,
然后将其传递到浏览器的
DocumentStream
属性。If you have filled the
result
variable (which seems to be a string), you should rather easily be able to modify the contents.Using e.g. Regular Expressions or HTML Agility Pack to search for the
and replace it with
then pass it to the
DocumentStream
property of your browser.我在 HttpWebrequest 返回的字符串中搜索了表单元素操作属性。这只显示从 httpWebRequest 接收的页面名称。我在代码中将其修改为完整的网址。
感谢您的评论凯文。这是有帮助的提示。
I searched the string being returned from the HttpWebrequest for the form element action attribute. This only showed the name of the page received from the httpWebRequest. I modified it in code to be the full url.
Thanks for your comment Kevin. This was the prompt that helped.