Solved

Wget and useragent options in java

Posted on 2011-09-08
4
378 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
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 200 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
A short article about problems I had with the new location API and permissions in Marshmallow
This video teaches viewers about errors in exception handling.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

12 Experts available now in Live!

Get 1:1 Help Now