java.io.IOException: Server returned HTTP response code: 403 for URL -


my code goes this:

url url; urlconnection uc; stringbuilder parsedcontentfromurl = new stringbuilder(); string urlstring="http://www.example.com/content/w2e4dhy3kxya1v0d/"; system.out.println("getting content url : " + urlstring); url = new url(urlstring); uc = url.openconnection(); uc.connect(); uc.getinputstream(); bufferedinputstream in = new bufferedinputstream(uc.getinputstream()); int ch; while ((ch = in.read()) != -1) {     parsedcontentfromurl.append((char) ch); } system.out.println(parsedcontentfromurl); 

however when trying access url through browser there no problem , when try access through java program, throws expection:

java.io.ioexception: server returned http response code: 403 url 

what solution?

add code below in between uc.connect(); , uc.getinputstream();:

uc = url.openconnection(); uc.addrequestproperty("user-agent",  "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"); 

however, nice idea allow types of user agents. keep website safe , bandwidth usage low.

some possible bad 'user agents' might want block server depending if don't want people leeching content , bandwidth. but, user agent can spoofed can see in example above.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -