Solved

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

Posted on 2013-12-30
1
550 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

807 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