java - Http Request for API -


i learning how use http request. following code return json. doing , print it. facing error. error given below.

import java.io.*; import java.net.*;  public class ziptester {      public static void main(string[] args) throws ioexception {         // todo auto-generated method stub          url stck = new url("http://www.zipfeeder.us/zip?key=ect9o9ta&zips=14623");         urlconnection yc = stck.openconnection();         bufferedreader in = new bufferedreader(new inputstreamreader(yc.getinputstream()));         string inputline;         string add="";         while ((inputline = in.readline()) != null) {             //system.out.println(inputline);             add=add+inputline;         }         in.close();         system.out.println(add);      }  } 

this error. in old machine code worked fine. got new machine week. same code not working. using jdk 1.7 , using jdk 1.8

exception in thread "main" java.io.ioexception: server returned http response code: 403 url: http://www.zipfeeder.us/zip?key=ect9o9ta&zips=14623     @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(unknown source)     @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source)     @ ziptester.main(ziptester.java:11) 

you can fix code adding user-agent header :

yc.addrequestproperty("user-agent",""); //to avoid 403 error 

the website had change access rules , request "user-agent".

the full code :

import java.io.*; import java.net.*;  public class ziptester {      public static void main(string[] args) throws ioexception {         // todo auto-generated method stub          url stck = new url("http://www.zipfeeder.us/zip?key=ect9o9ta&zips=14623");         urlconnection yc = stck.openconnection();         yc.addrequestproperty("user-agent",""); //to avoid 403 error         bufferedreader in = new bufferedreader(new inputstreamreader(yc.getinputstream()));         string inputline;         string add="";         while ((inputline = in.readline()) != null) {             add=add+inputline;         }         in.close();         system.out.println(add);      }  } 

i hope you.

regards.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -