AxShDocVw.AxWebBrowser.Navigate and object headers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am hosting a web application within a windows form using
MsHtmlHstInterop.dll.

I am opening the web browser window using:

object flags = 0;
object targetFrame = String.Empty;
object postData = String.Empty;
object headers = String.Empty;
this.WebBrowser.Navigate("http://www.mysite.com", ref flags, ref
targetFrame, ref postData, ref headers);

How can i pass an object to the web application. From what i've read it
seems it should be possible to pass an object in the "headers" parameter.

If this is the case, then how can i then access that object from within the
web application.

Any help much appreciated.
thanks.
 
Can you define "pass an object"?

Http headers are just string name/value pairs. If you can serialize
your object into a string, then I guess you could pass it in a header,
although the body (postData) would be more common (again, it would
need to be serialized). At the server you'd then just read the named
header from the Request, or the body, etc.

You might also note that in 2.0 there is a managed WebBrowser wrapper
in the System.Windows.Forms namespace.

What are you trying to do? There is a more object-based interaction
between a WebBrowser and a hosting application, but it is client-side
only - i.e. it allows your C# code and javascript code to talk to
eachother in both directions. It doesn't allow you to talk to the
server. Either regular http requests or web-services (SOAP etc) are
commonly used for this type of activity.

Marc
 
Hi Marc,

I am hosting a web application inside of a windows host.

I need to somehow send a custom object which i have built containing
properties methods etc. into it. Then from within the web application i will
make changes to my object properties. I then want to be able to access that
object again from the windows host.

Is there a way of performing this sort of functionality using the WebBrowser
control?

I have been checking out the following article on CodeProject:
http://www.codeproject.com/csharp/winformiehost.asp?df=100&forumid=15529&exp=0&select=1086020 which sort of gets at the sort of thing i need to do.

The thing is, i really need to be to move an object from the Windows host,
to the web child then back to the windows host again. The reasoning behind
this is that the host will then need to load other web apps into which the
same object will get passed into. There is an object which i need passed into
a sequence of web applications, and all this needs to be controlled by a
windows host.
 
The cited CodeProject article you refer is working client-side, not
client-server - meaning that the html at the client can talk to the
winform at the client, which is what I was discussing (window.external
aka WebBrowser.ObjectForScripting).

I think you need to clarify "web application" - do you mean the
web-server, or the html-client (including client-side javascript)?
There is a big difference. I suspect you mean the former (web-server),
but there is no /single/ (or standard) way of doing this:

You can automate things quite easily, but you need to be *very* clear
what data you are sending to where. A plain "object" isn't going to
work over the wire (client-server), but if there is an agreed
serialization format it might work; likewise, you can populate html
form elements and submit them from a host winform; this may be fine.
Or web-services / vanilla web-requests (without a browser) may be an
option.

Sorry if I'm not giving much of an answer, but the question is still
more-than-a-little vague...

Marc
 
Hi Marc,
I need the web form code behind, (web server), to be able to access an
object sent from the windows form.

so for example if my obect is a class called Car i may want to be able to
manipulate that object from web application's code behind. for example i
might want to set the Wheels property on my car object to 4.

I am interested in what you mentioned about an agreed serialization format.
Are you saying that if I passed my serialized Car object into the
AxShDocVw.AxWebBrowser.Navigate method, I can then deserialze it on the web
server? Can you give me pointer as to how this would work?

thanks,

many thanks,
 
Well, XML or JSON would be the obvious contenders, so (using XML for
simplicity) you would first serialize your object to a string:
<car manuf="fiat" model="punto" color="red" wheels="4">
<engine cyl="8" cc="125"/>
</car>
or whatever... the web-server can then either treat it "as is", or
deserialize into a known type (which might be similar-to, but
different-than then client's representation), and work on it. When
complete, the server re-serializes it and returns the updated fragment
in the response.

But - and I cannot stress this enough: what we have just described is
wrapped very neatly by the "web-services" umbrella - i.e. at the
simplest level with a server-side asmx file:

[WebMethod]
public Car DoSomething(Car value) {
if(value==null) throw new ArgumentNullException(value);
value.Wheels = 3; // why not...
// etc...
return value;
}

Your winform would *typically* consume this as a web-service
(web-reference), which exposes (via WSDL) a schema describing the
available operations (DoSomething) and objects (Car).

There are lots of ways of implementing a web-service; asmx/WSE3, WCF
[and other WS-* variants], etc - or for a different paradigm, REST
(which is also supported by WCF in .NET 3.5).

You can also use vanilla http-requests for this, but you don't need to
use a WebBrowser (or ShDocVw) for this - just a WebClient would do.
The following client-code will upload (POST) a Car as the body to an
http request, and interpret the server's response in the same manner
[with no WebBrowser in sight; *caveat* this code is not intended to
pair with the previous [WebMethod] server-side coded]:

static Car DoSomething(Uri address,Car car) {
XmlSerializer ser = new XmlSerializer(typeof(Car));
byte[] body, response;
using(MemoryStream ms = new MemoryStream()) {
ser.Serialize(ms, car);
body = ms.ToArray();
}
using (WebClient wc = new WebClient()) {
response = wc.UploadData(address, body);
}
using (MemoryStream ms = new MemoryStream(response)) {
return (Car) ser.Deserialize(ms);
}
}

Code like the above (probably in a different language/framework) goes
back may, many years...
Obviously you'd need matching code at the server-end. But this level
of plumbing is what web-services are designed to standardise and
automate.

Perhaps read up on web-services, web-references, etc.

Marc
 
Hi Marc,

thanks for your thorough response.
I see what you are getting at with web services, however I'm not sure it
could satisfy my requirements.

Using a webservice means that i would need to save my Car object to some
sort of physical location. If possible I would like to seamlessly move it
around from Windows form host to web application window, back to windows host
and then into another web form and so on. I am creating a workflow solution
and this object needs to be available in every hosted web application and
also from the host itself.

Hmm, i will give webservices a try as well as the serialization of the
object into the .Navigate parameter.

thanks
 
Using a webservice means that i would need to save my Car object to some
sort of physical location.

Er, why? I simply don't undertand what you mean here... there is no
need to save anything anywhere.
If possible I would like to seamlessly move it
around from Windows form host to web application window, back to windows host
and then into another web form and so on.

They are fundamentally different architectures. The best you can do is
an agreed serialization format, which is exactly what a web-service
does.
I am creating a workflow solution
and this object needs to be available in every hosted web application and
also from the host itself.

There is no *single* object here. Since they are different
architectures on different machines, you can only move a
representation of the Car. And that means it will get cloned. Hence
the need for the web-service (or whatever) to return the updated
representation of the Car when it has completed it's method.
Hmm, i will give webservices a try as well as the serialization of the
object into the .Navigate parameter.

At the most basic level, these two approaches amount to the same
thing. The big difference is that that a web-browser is designed to
show content (mainly HTML) to a human user, and a web-service is
designed to allow two *systems* to talk to each other.
Since the latter is what you are trying to do, this is the pattern
your design should favor. At the very least, if you don't want to use
SOAP web-services, then WebClient or HttpWebRequest are far better
options that a browser if you just want to send a Car (as xml or
whatever) to a Url that does something and returns the updated Car (as
xml or whatever).

Marc
 
Hi Marc,

The reason i think I need to save data to a location is that :

I need a windows host form which is loading web apps into the host window
according to a predefined order. Each one of those web apps uses an object
(for argument's sake, Car), and makes changes to it. If web app 1 says our
car has 3 wheels, web app 2 changes it to 5 wheels , then web app 3 needs to
know that Car has 5 wheels.

Each web app has a user interface and is doing lots of stuff to our car
object.
There will be multiple web applications.

If i make a change to Car inside web app 1, then i need that change to
persist, so that when the windows host calls web app 2, that change is still
there.

A webservice has no "state", and web apps and windows app cannot for example
share a Session object. How is it possible for web app 2 to know the current
state of Car using a webservice, if that webservice is not accessing that
information from somewhere.

The way envisage it, is that windows host loads web app 1. Web app 1
manipulates Car saving any changes to it via a webservice, storing it
(serialized to a string) in a database. When web app 1 is finished doing what
it needs to do, our host loads web app 2. Web app 2 loads our car from the
database via our webservice, deserializes it into an object and uses it,
manipulates it, makes changes to it via the web service. Changes get saved to
the db. and so on.......

What did you envisage taht web services could do in this scenario?

thanks,
CR
 
The reason i think I need to save data to a location is that :

OK - your requirements point to a central database, but this has
nothing todo with the fact that we are using web-services (rather than
any other transport). Absolutel WebBrowser.Navigate *will not* give
you anything extra here.

You could *just about* use remoting to do some of this, but it sounds
like stateless apps with a database.

As an aside - a web-service can be stateful if it chooses, either
using (for example) a WCF session, or by keeping things in memory on a
single server.

Marc
 
Back
Top