[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wget and useragent options in java

Posted on 2011-09-08
4
Medium Priority
?
389 Views
Last Modified: 2013-11-23
I want convert the below linux command into java a java program. How can i set useragent option in java??

>>>> wget www.somesite.com -U "option1 option2"

Note : here -U is useragent options



0
Comment
Question by:manugeek
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 36501539
Set http.agent System property. You can also set it as a header in URLConnection, but only the former is necessary
0
 
LVL 47

Accepted Solution

by:
for_yan earned 800 total points
ID: 36503892


This gives you some variants of code for wget-like Java programs:
http://stackoverflow.com/questions/238547/how-do-you-programmatically-download-a-webpage-in-java
and this is the recommendation for user agents from the last answer:

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
HttpURLConnection.setFollowRedirects(true);
// allow both GZip and Deflate (ZLib) encodings
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
String encoding = conn.getContentEncoding();
InputStream inStr = null;

// create the appropriate stream wrapper based on
// the encoding type
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
    inStr = new GZIPInputStream(conn.getInputStream());
} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
    inStr = new InflaterInputStream(conn.getInputStream(),
      new Inflater(true));
} else {
    inStr = conn.getInputStream();
}

Open in new window


User- agent:

conn.setRequestProperty ( "User-agent", "my agent name");

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36509531
Better to use the first solution i mentioned: you can pass it to the runtime with the ability to change it at will without recompilation
0
 

Author Comment

by:manugeek
ID: 36509728
Thanks CEHJ, it was helpful. I am rookie to java but will appreciate any working example or a link. Sorry i will not be able to give any more points has this question is marked closed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question