jmylrea
asked on
ASP.NET equivalent to ASPHTTP component.
Following is a a example ASP script that uses the ASPHttp component (basically you give it a URL and it returns the response string).
Set HttpObj = Server.CreateObject("AspHT TP.Conn")
HTTPObj.TimeOut = 10
theurl = "somevalidURL"
httpobj.url = theurl
strResult = HttpObj.GetURL
I need this exact function but in ASP.NET. I don't want to wrap the ASPHttp component or anything, I just want to use whatever in built funtions .NET has.
Set HttpObj = Server.CreateObject("AspHT
HTTPObj.TimeOut = 10
theurl = "somevalidURL"
httpobj.url = theurl
strResult = HttpObj.GetURL
I need this exact function but in ASP.NET. I don't want to wrap the ASPHttp component or anything, I just want to use whatever in built funtions .NET has.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is the solution I was after. Thanks mate you tweaked my search enough to find it.
Dim strURL As String = "http://someURL/default.htm"
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim streamReader As System.IO.StreamReader
Dim strHTML As String
objWebRequest = CType(System.Net.WebReques t.Create(s trURL), System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetRes ponse(), System.Net.HttpWebResponse )
streamReader = New System.IO.StreamReader(obj WebRespons e.GetRespo nseStream)
strHTML = streamReader.ReadToEnd
Response.Write(strHTML)
streamReader.Close()
objWebResponse.Close()
objWebRequest.Abort()
Dim strURL As String = "http://someURL/default.htm"
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim streamReader As System.IO.StreamReader
Dim strHTML As String
objWebRequest = CType(System.Net.WebReques
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetRes
streamReader = New System.IO.StreamReader(obj
strHTML = streamReader.ReadToEnd
Response.Write(strHTML)
streamReader.Close()
objWebResponse.Close()
objWebRequest.Abort()
You may need to read through some of the documentation pages starting with that one. It's not quite as simple as popping AspHTTP.Conn....
But something along the lines of (assuming C# syntax, and without testing any of this):
System.Net.HttpWebRequest myReq = (HttpWebRequest)System.Net .WebReques t.Create(" somevalidu rl");
myReq.Timeout = 1000; // milliseconds
System.Net.WebResponse myResp = myReq.GetResponse();
System.IO.Stream strm = myResp.GetResponseStream() ;
System.Text.Encoding enc = System.Text.Encoding.GetEn coding("ut f-8");
StreamReader readStream = new StreamReader(myResp, enc);
StringBuilder sbResult = new StringBuilder();
// Grab 256 chars at a time
Char[] read = new Char[256];
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// add the 256 characters into the StringBuilder
sbResult.Append(read);
count = readStream.Read(read, 0, 256);
}
// dump the StringBuilder contents into a string variable
string strResult = sbResult.ToString();
// free up the stream's resources
myResp.Close();
For a list of the properties and methods of the WebRequest class, see http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnetwebrequestmemberstopic.asp
For a list of the properties and methods of the WebResponse class, see http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnetwebresponsememberstopic.asp
But something along the lines of (assuming C# syntax, and without testing any of this):
System.Net.HttpWebRequest myReq = (HttpWebRequest)System.Net
myReq.Timeout = 1000; // milliseconds
System.Net.WebResponse myResp = myReq.GetResponse();
System.IO.Stream strm = myResp.GetResponseStream()
System.Text.Encoding enc = System.Text.Encoding.GetEn
StreamReader readStream = new StreamReader(myResp, enc);
StringBuilder sbResult = new StringBuilder();
// Grab 256 chars at a time
Char[] read = new Char[256];
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// add the 256 characters into the StringBuilder
sbResult.Append(read);
count = readStream.Read(read, 0, 256);
}
// dump the StringBuilder contents into a string variable
string strResult = sbResult.ToString();
// free up the stream's resources
myResp.Close();
For a list of the properties and methods of the WebRequest class, see http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnetwebrequestmemberstopic.asp
For a list of the properties and methods of the WebResponse class, see http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnetwebresponsememberstopic.asp
StreamReader.ReadToEnd -- that's what I was missing. :-O
Good stuff. :-)
Good stuff. :-)
ASKER