java


why my code is returning 403 forbidden acess


I am trying to make a http get request using java ,when i am executing the code ,i am getting 403 forbiddencode .Is there any way to get rid of that ?my code is
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class Http {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String [] args){
Http http=new Http();
try {
http.get();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void get() throws Exception{
URL ob=new URL("http://www.google.com/search?q=vamsi");
HttpURLConnection con=(HttpURLConnection) ob.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("USER_AGENT", USER_AGENT);
int responseCode=con.getResponseCode();
System.out.println("Response code is "+responseCode);
BufferedReader buf=new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
StringBuffer response =new StringBuffer();
while((input=buf.readLine())!=null){
response.append(input);
}
buf.close();
System.out.println(response.toString());
}
}
Google Search will deny your request if you are doing it outside of their API and Terms of Service.
$ wget http://www.google.com/search?q=vamsi
Resolving www.google.com (www.google.com)... 74.125.225.212, 74.125.225.210, 74.125.225.211, ...
Connecting to www.google.com (www.google.com)|74.125.225.212|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2014-02-01 23:21:08 ERROR 403: Forbidden.
They may be looking at your User-Agent header. If you add one that resembles a browser, it may work, though you are circumventing the Terms of Service and seeking unsupported behavior. (Sorry, I too tried this once.)
See also Why does Google Search return HTTP Error 403?
FYI, this is a common restriction of major search engines (Google, Bing, Yahoo). There are some that you can query programatically; you will have to use those.
This is the solution:
con.setRequestProperty("User-Agent", USER_AGENT);

Related Links

Error drools - java.lang.NoClassDefFoundError: org/drools/event/KnowledgeRuntimeEventManager
Java SOAP Client Data Lost
How do I prevent the text from wrapping in a alert dialog?
ADT - How do I graphicly edit a scrollview that is bigger than the screen size?
URL and URI difference when accessing web service in Java
Maven Build in Eclipse without using Command prompt
Which default logback properties file does Maven use?
What Connection is this?
HTTP Status 500 - javax.servlet.ServletException file not found
Compiler error for string switch case label
setter method not working in Spring?
Error during publishing of web service for insertion and seaching of data in database
Translate a Java expression/formula into Objective-C
Convert 'Apr 26 2014' to '2014-04-26' in grails
Android GPS not working in MyService
Java java.lang.ArithmeticException finding multiples of a number

Categories

HOME
bluetooth
pypi
webpack
openstack
image-processing
deezer
plone
view
relayjs
sd-card
routes
kalman-filter
adb
at-command
malloc
jgroups
enterprise-library-5
opengl-es-2.0
constraint-programming
primary-key
communication
adobe-analytics
spring-xd
rascal
postgres-xl
remote-access
visual-studio-2005
hex-editors
vaadin7
django-simple-history
invantive-sql
apache-metamodel
text-rendering
textfield
paging
opencover
graphicsmagick
excel-2007
jndi
claims-based-identity
wijmo
buildbot
semantic-analysis
kannel
entitlements
elasticsearch-net
replaceall
newline
.net-4.0
karaf
framemaker
subset-sum
android-kernel
vxworks
swisscomdev
service-discovery
hilbert-curve
azure-ml
greenrobot-eventbus
tasker
graphenedb
avro4s
quadratic-programming
jvm-languages
modelmapper
businessworks
wso2carbon
filepicker
react-scripts
rdfs
recursive-datastructures
pg-dump
imanage
mcafee
topbeat
cubes
vmware-tools
infix-notation
firebaseui
gstreamer-0.10
nsviewcontroller
prettytensor
bluegiga
google-places
endeca-workbench
cyclomatic-complexity
citrus-pay
pundit
embedded-code
apache-commons-net
tt-news
qcodo
eol
uv-mapping
commoncrypto
easy-install
sharp-repository
nsnetservice
cascalog
osql
assembly-loading
xmlspy
mysql-error-1005
blitz++
asp.net-mvc-areas
gamequery
wise
icanhaz.js
gin
jquery-ui-droppable
xsdobjectgen
perfect-hash
sector
geneva-server
signal-handling

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile