{"id":750,"date":"2003-10-25T12:57:28","date_gmt":"2003-10-25T16:57:28","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=750"},"modified":"2003-10-25T12:57:28","modified_gmt":"2003-10-25T16:57:28","slug":"fail-safe-amazon-image-using-java-c-coldfusion","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/10\/25\/fail-safe-amazon-image-using-java-c-coldfusion\/","title":{"rendered":"Fail-Safe Amazon Image&#8230; using Java, C# &amp; ColdFusion"},"content":{"rendered":"<p>Paul of <a href=\"http:\/\/www.onfocus.com\">onfocus.com<\/a> fame (and the fabulous <a href=\"http:\/\/www.onfocus.com\/snap\/\">SnapGallery<\/a> tool) <a href=\"http:\/\/www.onfocus.com\/index.asp?xml=2003_10_01_past.xml#3340\">wrote<\/a> an <a href=\"http:\/\/www.oreillynet.com\/pub\/a\/javascript\/2003\/10\/21\/amazonhacks.html\">article for the O&#8217;Reilly Network<\/a> recently that (I think) was an excerpt of his recently released book &#8220;<a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596005423\/cephasnet-20\">Amazon Hacks<\/a>&#8220;.  Anyway, he shows how you can check to see if an image exists on amazon.com using ASP, Perl, and PHP and I thought it would be fun to show how to do the same thing in Java, C# and ColdFusion. His examples were all functions of the form:<br \/>\n<code><br \/>\nFunction hasImage(imageUrl)<br \/>\n<\/code><br \/>\nso I&#8217;m following that style. In Java you&#8217;d end up with something like this:<br \/>\n<code><br \/>\npublic static boolean hasImage(String url) {<br \/>\nboolean result = false;<br \/>\n&nbsp;&nbsp;try {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;URL iurl = new URL(url);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;uc.connect();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if (uc.getContentType().equalsIgnoreCase(\"image\/jpeg\")) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = true;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;uc.disconnect();<br \/>\n&nbsp;&nbsp;} catch (Exception e) {<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;return result;<br \/>\n}<br \/>\n<\/code><br \/>\nIn C#, almost the exact same thing:<br \/>\n<code><br \/>\npublic static Boolean HasImage(String url) {<br \/>\n&nbsp;&nbsp;Boolean result = false;<br \/>\n&nbsp;&nbsp;try {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;WebResponse res = webreq.GetResponse();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if (res.ContentType == \"image\/jpeg\") {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = true;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;response.Close();<br \/>\n&nbsp;&nbsp;} catch {<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;return result;<br \/>\n}<br \/>\n<\/code><br \/>\nand then in ColdFusion:<br \/>\n<code><br \/>\n&lt;cffunction name=\"hasImage\" returntype=\"boolean\" output=\"no\"&gt;<br \/>\n&nbsp;&nbsp;&lt;cfargument name=\"imageUrl\" type=\"string\" required=\"yes\"&gt;<br \/>\n&nbsp;&nbsp;&lt;cfhttp url=\"#imageURL#\" method=\"GET\"&gt;<br \/>\n&nbsp;&nbsp;&lt;cfif cfhttp.responseHeader[\"Content-Type\"] EQ \"image\/jpeg\"&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfreturn true&gt;<br \/>\n&nbsp;&nbsp;&lt;cfelse&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfreturn false&gt;<br \/>\n&nbsp;&nbsp;&lt;\/cfif&gt;<br \/>\n&lt;\/cffunction&gt;<br \/>\n<\/code><br \/>\nThe full source for all these examples are available:<\/p>\n<p>&middot; <a href=\"\/images\/files\/Amazon.java\">Amazon.java<\/a><br \/>\n&middot; <a href=\"\/images\/files\/Amazon.cs\">Amazon.cs<\/a><br \/>\n&middot; <a href=\"\/images\/files\/amazon.cfm\">amazon.cfm<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Paul of onfocus.com fame (and the fabulous SnapGallery tool) wrote an article for the O&#8217;Reilly Network recently that (I think) was an excerpt of his recently released book &#8220;Amazon Hacks&#8220;. Anyway, he shows how you can check to see if an image exists on amazon.com using ASP, Perl, and PHP and I thought it would &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/10\/25\/fail-safe-amazon-image-using-java-c-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Fail-Safe Amazon Image&#8230; using Java, C# &amp; ColdFusion<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,7,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/750"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}