Supporting XMLHTTP functionality in .NET

Grant Killian was converting a classic ASP application into .NET.  He wasn’t satisfied with interop and was wondering about a replacement for the xmlhttp approach from MSXML (pre-.NET).  Here is the code:



XMLHTTP obj = new MSXML2.XMLHTTP();
obj.open( “POST”, strURL, false, strUid, strPwd );
obj.setRequestHeader( “Content-Type”,”application/x-www-form-urlencoded” );
obj.send( strXMLMessage );
if( obj.status != 200 ) {
//unsuccessful . . .
}
else {
//success . . .
obj.responseText;
}
obj = null;


Paul suggested looking at System.Net.HttpWebRequest and System.Net.HttpWebResponse.  To make a long story short, here’s the .NET port of xmlhttp:



HttpWebRequest req = ( HttpWebRequest )WebRequest.Create( strUrl );
req.ContentType = “application/x-www-form-urlencoded”;
req.Method= “POST”;
System.IO.Stream stream = req.GetRequestStream();
string strXML = getXMLString();
byte[] arrBytes = System.Text.ASCIIEncoding.ASCII.GetBytes( strXML );
stream.Write( arrBytes, 0, arrBytes.Length );
stream.Close();
WebResponse resp = req.GetResponse();
Stream respStream = resp.GetResponseStream();
StreamReader rdr = new StreamReader( respStream, System.Text.Encoding.ASCII );
string strResponse = rdr.ReadToEnd();

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

4 Responses to Supporting XMLHTTP functionality in .NET

  1. Darrell says:

    Dave – you’re the first to notice. :)

  2. dave says:

    thanks Darrel, I fugured that because there was no function named getXMLString() in .NET framework, finally I got it working

    Dim myReq As HttpWebRequest

    myReq = CType(WebRequest.Create("http://www.frozendev.com/"), HttpWebRequest)

    myReq.ContentType = "application/x-www-form-urlencoded"

    myReq.Method = "GET"

    Dim myRes As WebResponse = myReq.GetResponse()

    Dim respStream As System.IO.Stream = myRes.GetResponseStream()

    Dim reader As System.IO.StreamReader = New System.IO.StreamReader(respStream, System.Text.Encoding.ASCII)

    Dim strResponse As String = reader.ReadToEnd

    myRes.Close()

    Response.Write(strResponse.ToString)

  3. Darrell says:

    Dave – if I remember correctly, it was a simple util function that just took the stream and read the XML using an XmlReader and returned the string.

  4. dave says:

    what’s getXMLString();?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>