wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes HttpUrlConnection is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpUrlConnection is not working" Watch "HttpUrlConnection is not working" New topic
Author

HttpUrlConnection is not working

enkhbat dash
Greenhorn

Joined: Aug 12, 2008
Posts: 11
When I copy and paste http://www.google.com/search?q=test in address bar, it works perfectly. But when I using HttpUrlConnection to connect it, it doesn't work. Here is my code.




When I copy and paste link to address bar, it's accessing web via get method right? And code uses get method, too. Any suggestion and help?


Be nice!
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hi,
I think you should try using

instead of
.

Regards,
Amit
enkhbat dash
Greenhorn

Joined: Aug 12, 2008
Posts: 11
Thank you for quick reply, but changing by that doesn't make difference.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The HttpURLConnection uses "Java [Version]" as the default user agent.

Since Google is incredibly smart, it blocks requests that come from a java program.

You need to set the user agent to "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)" or the like


JDBCSupport - An easy to use, light-weight JDBC framework -
enkhbat dash
Greenhorn

Joined: Aug 12, 2008
Posts: 11
Thanks, can you share me valid agent names?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Here...

enkhbat dash
Greenhorn

Joined: Aug 12, 2008
Posts: 11
Thanks again. It works. You helped me a lot.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
No problem.

If you want to be stylish, turn the static final variables above into an enum (better approach).
enkhbat dash
Greenhorn

Joined: Aug 12, 2008
Posts: 11
Okay, I'll keep in mind.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HttpUrlConnection is not working
 
Similar Threads
HttpUrlConnection is not working
URL connection connectiontimeout and readtimeout issues
How to specify the name of the database
HttpUrlConnection is not working
post url connection not working