Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-30
1
Medium Priority
?
567 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

971 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