Solved

Wget and useragent options in java

Posted on 2011-09-08
4
372 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 50 total points
Comment Utility
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 200 total points
Comment Utility


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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now