Solved

ASP.NET/VB: Get status code, content-type and loading time when downloading remote file

Posted on 2013-12-30
1
536 Views
Last Modified: 2013-12-31
Using ASP.NET/VB, how can I get the status code, the content-type and the time to download of a remote file?
<%@ Page Language="VB" AutoEventWireup="false" %>
<%
    Dim data As String
    Using WC As New System.Net.WebClient()
     data = WC.DownloadString("http://www.experts-exchange.com/")
    End Using

    Response.Write("Status: 200")
    Response.Write( "<br>" )
    Response.Write("Content-Type: text/html;charset=UTF-8")
    Response.Write( "<br>" )
    Response.Write("Milliseconds to load: 123")
    Response.Write( "<br>" )
%>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39747911
I think the real .NET experts are all on holiday ;-) so I'll give this a go but a little apprehensive partly because I don't know if there's a specific reason you're using WebClient(). If there is then my proposal might not be applicable at all...

The basis of this alternative is that using HttpWebRequest/Response you can get to most of what you need from standard properties, only the elapsed time I couldn't find but that can be calculated.

There's one more catch: if you want to allow for images and such (binary download) then you need other code to read/save the stream, using a byte array. I tested that with some code I found on the 'net but got a garbled file so will have to investigate further if needed.

Can you give this a go:
Dim data As String
    Dim ticks As Long = Now.Ticks
    Dim url As String = "http://www.experts-exchange.com/"
    Dim WC As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
    Using rsp As System.Net.HttpWebResponse = WC.GetResponse
        Dim sr As New System.IO.StreamReader(rsp.GetResponseStream)
        data = sr.ReadToEnd
        Dim ms As Long = Now.Ticks - ticks

        Response.Write("Status: " & rsp.StatusCode)
        Response.Write("<br>")
        Response.Write("Content-Type: " & rsp.ContentType)
        Response.Write("<br>")
        Response.Write("Milliseconds to load: " & (ms / TimeSpan.TicksPerMillisecond))
        Response.Write("<br>")
        Response.Write("Data length: " & data.Length)
        Response.Write("<br>")
    End Using
    WC = Nothing

Open in new window

BTW: not sure what your goal is with this but be aware it only reads the html and is not a good measure for the total loading speed of a page.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now