Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

632 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