0

I need to parse content type of typed URL:

  1. if it's an image then do something.
  2. if it's a web page then scan this page for images and fill array with images' SRC attributes (and order it by size).

How can i do this using only JS? How can i do this using only ASP.NET and C#?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • You want to do it ONLY with Javascript and ONLY with c#???? – Shoban Dec 21 '11 at 08:45
  • You should get the headers, after that everything is easy. http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript – noob Dec 21 '11 at 08:47
  • @Shoban Yes. java script: you have a string (i.e. url). You must parse it and fill Array. c#: you have a string (i.e. url). You must parse it and fill ArrayList. –  Dec 21 '11 at 08:50

3 Answers3

0

You can't if the URL is an image for sure. Either you go for url.match( /\.jpg|\.gif|\.png/ ) === nil (and other extensions, of course), or you make a GET call to the URL and look and the Content-Type in the response header.

Julio Santos
  • 3,837
  • 2
  • 26
  • 47
0

i'm not familiar with java but try this: http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Atom Vayalinkal
  • 2,642
  • 7
  • 29
  • 37
0

Here is how you can do it in C# (by actually making a request and inspecting the content type of the response):

    public static bool IsImage(string url)
    {
        WebRequest req = HttpWebRequest.Create(url);

        try
        {
            WebResponse resp = req.GetResponse();
            return resp.ContentType.StartsWith("image");
        }
        catch { }

        return false;
    }
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95