C#/Winform:在HTML页面中输入数据,提交表单
我有一个带有BackgroundWorker 的Winform。 其中,BackgroundWorker 必须对页面进行 HTTP 调用,填写一些数据,提交表单,并检索“单击”提交按钮后返回的 HTML。 我在执行此操作时遇到了许多障碍:
- 无法发布数据,因为目标网络服务器不 405 支持该方法。
- 再次无法使用 WebClient.UploadValues,因为网络服务器不支持 POST。
- 无法使用 WebBrowser 控件,因为 BackgroundWorkers 在 COM 互操作方面很糟糕,并且会引发异常,表明它必须位于 STA 线程(单线程单元)中
- 无法运行另一个单独的线程,因为 BW 必须坐下来等待结果才能继续(不能,或者至少我不知道一种不会崩溃的方法)
- 无法更改线程的 ApartmentState,因为它是一个 BackgroundWorker,如果被告知去,它会抛出异常STA 模式
我应该如何解决这个问题?
[编辑]:应用程序入口点已使用 [STAThread] 属性进行标记。
I have a Winform with a BackgroundWorker. The BackgroundWorker, among other things, has to make an HTTP call to a page, fill out some data, submit the form, and retrieve the HTML that comes back after "clicking" the submit button. I've run into a number of roadblocks while doing this:
- Can't POST the data because the target webserver doesn't 405 support that method.
- Can't use a WebClient.UploadValues, again, because the webserver doesn't support POST.
- Can't use a WebBrowser control because BackgroundWorkers suck at COM Interop and an exception is thrown that says it must be in a STA thread (Single-Threaded Apartment)
- Can't run another seperate thread because the BW has to sit and wait for the result before it can continue (Can't, or at least I don't know a way to do this that won't crash)
- Can't change the ApartmentState of the thread because it's a BackgroundWorker and it throws if told to go to STA mode
What should I do to resolve this?
[Edit]: The app entrypoint is already tagged with the [STAThread] attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用 WebClient.UploadValues 将 Method 参数设置为“GET”?
Have you tried using WebClient.UploadValues with the Method argument set to "GET" ?
在这种情况下,哪个网络服务器不支持 POST? 类似的 HTML 表单有什么作用? 是 POST 还是 GET? 只要做同样的事情..我怀疑 WebClient 或 HttpWebRequest 会很好地完成这项工作。
What web-server is it that doesn't support POST in this scenario? What does the comparable HTML form do? A POST or a GET? Just do the same.. I suspect that WebClient or HttpWebRequest will do the job fine.
不使用后台工作者吗?
如果你这样做,你可以将 ApartmentState 设置为你想要的。 只需记住将数据推回到任何表单控件时调用/BeginInvoke即可。
Don't use a background worker?
If you do that you can set the ApartmentState to what you want. Just remember to Invoke/BeginInvoke when pushing data back to any Form controls.
我很不确定你到底想问什么。 如果您运行后台进程并且失败,您可以通过 RunWorkerCompletedEvent 报告此情况。 然后,如果您查看事件参数,您可以判断该过程是否成功(通过 RunWorkerCompletedEventArgs.Error 属性)。
根据错误,您可以重新启动请求或向用户显示错误。
我希望我没有完全偏离轨道。
I am quite unsure what are you really asking about. If you run a background process and it fails you get this reported via RunWorkerCompletedEvent. If you then have a look at the eventarguments you can tell whether the process was succesfull or not (via RunWorkerCompletedEventArgs.Error property).
Depending on the error you can then relaunch your request or display the error to the user.
I hope I am not completely off the track.