Question

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:

  1. Can't POST the data because the target webserver doesn't 405 support that method.
  2. Can't use a WebClient.UploadValues, again, because the webserver doesn't support POST.
  3. 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)
  4. 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)
  5. 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.

Was it helpful?

Solution

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.

OTHER TIPS

Have you tried using WebClient.UploadValues with the Method argument set to "GET" ?

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top