Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

17 Experts available now in Live!

Get 1:1 Help Now