Darrell Norton's Blog [MVP]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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();


Posted Thu, Jul 17 2003 4:48 PM by Darrell Norton

[Advertisement]

Comments

dave wrote re: Supporting XMLHTTP functionality in .NET
on Sat, Feb 26 2005 9:12 AM
what's getXMLString();?
Darrell wrote re: Supporting XMLHTTP functionality in .NET
on Mon, Feb 28 2005 6:04 AM
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.
dave wrote re: Supporting XMLHTTP functionality in .NET
on Wed, Mar 2 2005 6:31 AM
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)
Darrell wrote re: Supporting XMLHTTP functionality in .NET
on Wed, Mar 2 2005 10:44 AM
Dave - you're the first to notice. :)
Devlicio.us