简体   繁体   中英

replicate wget command in java

i have this command:

wget -O prova.csv  --header="prova-user: guest" --header="prova-passwd: guest" 
"http://www.....................80&albedo=0.2&horizon=1"

i want to do a batch scheduled in Java but I can not connect. When I try to take the imputstream return me this error:

ERROR message -8: Unregistered IP address

This is my piece of code:

URL myURL = new URL(url);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "guest:guest";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);

// Show page.
BufferedReader reader = null;
try {
     reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "UTF-8"));
     for (String line; ((line = reader.readLine()) != null);) {
            System.out.println(line);
     }
 } finally {
       if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}

is it possible? and how can I do it?

Thanks in advance

You had provided 2 completely different commands.

The first is a wget that send in HTTP headers a sort of authentication infos, and GET a result.

The second is a java program that perform an HTTP request in POST with basic authentication.

If the first command is working, than you should forget about the basic authentication and set the proper HTTP headers as you did in the wget command.

I don't know why you try a POST, if the wget looks as a normal GET request.

Just use a GET request in java too.

And it should work.

About the error, I suppose is the server that sent you such error message.

So it could be as you haven't correctly authenticated.

But it is a strange error, I'm expecting such kind of error if the server have a white list of IP addresses allowed to connect.

Are you running the wget and the java code on the same server?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM