Solved

how to check if a URL is valid before making a connection

Posted on 2006-10-24
2
197 Views
Last Modified: 2010-04-16
Hi guys

i have this code in which I check whether there is an image stored on the server or a link to an image is provided. Now what I need is that if the image link doesn't go to a valid image then it shouldn't show the image. Also when I write some random word a s a link to the image such as 'jsadsakd' it throws an error and doesn't display the whole page at all


her is my code
----------------------
 <% if(chapter_sponsor.getImage_size() > 0){%>
                                <td>
                                    <a href ="<%= chapter_sponsor.getImage_link() %>" target="_blank" ><img src="attachments/sponsors/<%=chapter_ID%><%=chapter_sponsor.getSponsor_ID()%>" border="0" width="200" height="100"></a>      
                                </td>
                                <%}else if(!checkNull(chapter_sponsor.getSponsor_image_url()).equals("")){
                                          URL img = new URL(chapter_sponsor.getSponsor_image_url());
                                          try{
                                                HttpURLConnection con = (HttpURLConnection )img.openConnection();
                                                if(con.getResponseCode()!=HttpURLConnection.HTTP_NOT_FOUND){%>
                                                      <td>
                                                      <a href ="<%= chapter_sponsor.getImage_link() %>" target="_blank" ><img src="<%=chapter_sponsor.getSponsor_image_url()%>" border="0" width="200" height="100"></a>      
                                                      </td>
                                         <%}}catch(IOException e)
                                    { %>
                                                <td>
                                                      <a href ="<%= chapter_sponsor.getImage_link() %>" target="_blank" ><img src="http://www.ccim.com/common/images/missing_photo.gif" border="0" width="200" height="100">
                                                </td>

                              <% }}%>
0
Comment
Question by:huzefaq
2 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
 Is this validation to be performed by the server?  You might try retrieving the image (on the server) before you put the image URL in the src attribute.
0
 
LVL 3

Accepted Solution

by:
royce2020 earned 500 total points
Comment Utility
You could try checking the link for the substing ".jpg"  for instance, the following function will return true if the link ends in a valid image extention, and false if something else (you could also add elements to ArrExtentions for more image types.):

function RealImg(strLink as string)
    ret = false
    ArrExtentions = array (".jpg",".jpeg",".gif",".png")
    for i = 0 to ubound (ArrExtentions) -1
        if lcase(left(strLink,(len(ArrExtentions(i))))) = ArrExtentions(i) then ret = true
    next i
    RealImg = ret
end function
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

12 Experts available now in Live!

Get 1:1 Help Now