java - Can't read a google static map via HtmlUnit -
i trying read static map google maps on following url.
this works fine web browser, when try htmlunit unexpectedpage
result. know means?
according javadoc of unexpectedpage
, you're receiving unexpectedpage
because server returns unexpected content type. if check returned header in htmlunit can see contains: content-type=image/png
here's little application retrieves image url:
import java.awt.image; import java.io.ioexception; import java.io.inputstream; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import com.gargoylesoftware.htmlunit.unexpectedpage; import com.gargoylesoftware.htmlunit.webclient; /** small test application used fetch map. */ public class fetchmapswingapp extends jframe { /** serial id. */ private static final long serialversionuid = 1920071939468904323l; /** * default constructor. */ public fetchmapswingapp() { // make sure application closes correctly setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); // map trying read string url = "http://maps.googleapis.com/maps/api/staticmap?center=55.690815,12.560678&zoom=15&size=400x500&sensor=false"; // fetch image image image = fetchmap(url); // add image jframe , resize frame. add(new jlabel(new imageicon(image))); pack(); } /** * fetch image on given url. * * @param url * image location * @return fetched image */ private image fetchmap(string url) { image image = null; webclient webclient = new webclient(); webclient.setthrowexceptiononscripterror(false); try { // url returns png file! unexpectedpage page = webclient.getpage(url); inputstream inputstream = page.getinputstream(); // read stream image image = imageio.read(inputstream); } catch (ioexception e) { e.printstacktrace(); } return image; } /** * start of application. * * @param args * arguments main method */ public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new fetchmapswingapp().setvisible(true); } }); } }
Comments
Post a Comment