views:

2053

answers:

5

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.

A: 

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.

Quibblesome
That kinda defeats the purpose of using a BackgroundWorker - the UI thread would be doing all the work.
Marc Gravell
Uh-what-eh? No you don't need a BackgroundWorker object to do things in the background. You can use ThreadPool.QueueUserWorkItem or new Thread(SomeMethod).Start(). In the case of the latter you can set the Apartment state.
Quibblesome
+2  A: 

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

R. Bemrose
Yes I did, and got "System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type."
tsilb
A: 

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.

Marc Gravell
This is an internal app with two textboxes and a button to log in. The form itself has a javascript event onClick that forces a postback (wow, that's unusual); WebClient throws as mentioned in point 2; how does one HttpWebRequest with a payload and not using method POST?
tsilb
A: 

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.

Tomas Pajonk
Yes, but this requirement (call page, log in, return result at landing page) is only a tiny piece of what the background thread actually does... In fact it's one part of about 20 different things that have to be done on this thread. I'd use another BackgroundWorker but it'd have the same problems.
tsilb
A: 

Even Cassini (ASp.NET development server) supports POST. What web server are you using that does not support POST?

Mercede