[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ASP.NET equivalent to ASPHTTP component.

Posted on 2004-08-26
5
Medium Priority
?
617 Views
Last Modified: 2008-01-09
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("AspHTTP.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.
0
Comment
Question by:jmylrea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 500 total points
ID: 11908888
Sounds like you may be looking for the System.Net.HttpWebRequest class.  Details at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnethttpwebrequestclasstopic.asp
0
 

Author Comment

by:jmylrea
ID: 11908904
Yeh I realise it may have something to do with that class. But I can't work out the exact syntax?
0
 

Author Comment

by:jmylrea
ID: 11908989
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.WebRequest.Create(strURL), System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetResponse(), System.Net.HttpWebResponse)

streamReader = New System.IO.StreamReader(objWebResponse.GetResponseStream)

strHTML = streamReader.ReadToEnd
Response.Write(strHTML)

streamReader.Close()
objWebResponse.Close()
objWebRequest.Abort()
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11909057
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.WebRequest.Create("somevalidurl");
    myReq.Timeout = 1000;  // milliseconds
    System.Net.WebResponse myResp = myReq.GetResponse();
    System.IO.Stream strm = myResp.GetResponseStream();
    System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-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
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11909064
StreamReader.ReadToEnd -- that's what I was missing.  :-O

Good stuff.  :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question