MSXML COM Object

R

rajkiranpro

I tried to create an object of xmlhttpclass.

MSXML2.XMLHTTPClass xmlclass = new MSXML2.XMLHTTPClass();
xmlclass.open("POST", txtURL.Text, false, null, null);
xmlclass.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlclass.send(txtRequest.Text);
System.IO.StreamReader sr = new
System.IO.StreamReader((System.IO.Stream)xmlclass.responseStream);
txtREsponse.Text = sr.ReadToEnd();
sr.Close();

however I couldn't get the response stream. I get the following error while
doing so

Unable to cast COM object of type 'System.__ComObject' to class type
'System.IO.Stream'. Instances of types that represent COM components cannot
be cast to types that do not represent COM components; however they can be
cast to interfaces as long as the underlying COM component supports
QueryInterface calls for the IID of the interface.

Please help me


Regards
Rajkiran
 
J

Jeroen Mostert

rajkiranpro said:
I tried to create an object of xmlhttpclass.
Why? .NET has perfectly serviceable classes for this. If you are not just
copying old VB/ASP code and trying to port it, I'm very interested in the
kind of scenario that would need this.
MSXML2.XMLHTTPClass xmlclass = new MSXML2.XMLHTTPClass();
xmlclass.open("POST", txtURL.Text, false, null, null);
xmlclass.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlclass.send(txtRequest.Text);
System.IO.StreamReader sr = new
System.IO.StreamReader((System.IO.Stream)xmlclass.responseStream);
txtREsponse.Text = sr.ReadToEnd();
sr.Close();
The .NET equivalent is

WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
txtResponse.Text = webClient.UploadString(txtURL.Text, txtRequest.Text);

Much simpler, no?

If you are going to .NET from the unmanaged world, I recommend reading a
book on getting started with .NET first, to get an idea of what the
framework supports. Trying to force in unmanaged concepts is a recipe for
disaster.
Unable to cast COM object of type 'System.__ComObject' to class type
'System.IO.Stream'. Instances of types that represent COM components
cannot be cast to types that do not represent COM components; however
they can be cast to interfaces as long as the underlying COM component
supports QueryInterface calls for the IID of the interface.
First of all, the XMLHTTP object has a "responseText" property that allows
you to read the response directly as a string, so no need to muck about with
streams even if you do want to use XMLHTTP for some reason I can't fathom.

Second, if you absolutely do need to get the stream of XMLHTTP, try
Marshal.GetObjectForNativeVariant(xmlclass.responseStream). This *should*
return a Stream instance, but I haven't tested it (I'm not sure it supports
IStream variants).
 
R

rajkiranpro

thanks for the answer.

yes I implemented first using the webclient and it worked perfect.

I just wanted to try the same using MSXML com object.

and I`ll try using the Marshal
 
A

Anthony Jones

rajkiranpro said:
thanks for the answer.

yes I implemented first using the webclient and it worked perfect.

I just wanted to try the same using MSXML com object.

and I`ll try using the Marshal

Could you elaborate why you would using MSXML (which in this case is acting
merely as a way to use the WinINet) stack, when the .NET Fx has much better
API for this sort of thing?
 
R

rajkiranpro

just tried how to use com components under .net


Anthony Jones said:
Could you elaborate why you would using MSXML (which in this case is
acting merely as a way to use the WinINet) stack, when the .NET Fx has
much better API for this sort of thing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top