Jump to content

loging into the web ui with java


Ludjer

Recommended Posts

i am trying to login to the web ui to get data with java but i keep on getting the same errors, can some one help me

--------------------Configuration: <Default>--------------------
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://username:password@localhost:8668/gui/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1153)
at torrentstats.main(torrentstats.java:20)

Process completed.

the error is that it does not connect using the right username and password

CODE 410 = HTTP Error 401 Unauthorized Explained

here is my code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class torrentstats {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://usename:password@localhost:8668/gui/?list=1");
URLConnection yahooConnection = yahoo.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yahooConnection.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
}

thx in advance

Link to comment
Share on other sites

Using username:password in a url is a handy trick to tell a browser (IE/Fx2/Opera) to use those as the Authentication part of the header it sends when requesting the page. This trick will not work in programming languages that allow you to query a webpage. Unless you have the option to adjust or add to the header the program sends you cannot automatically login to the webui.

The header that has to be add is

Authorization: Basic loginstring

Where the loginstring is the base64 encoded "username:password" (without quotes but with the :).

For example username john and password god would require the header:

Authorization: Basic am9objpnb2Q=

This handy web base64 encoder/decoder might be of help:

http://makcoder.sourceforge.net/demo/base64.php?plain=john:god&process=Encode

Link to comment
Share on other sites

here is my function in java using this

  static String geturl(String urlreq,String username,String password) throws Exception
{
String filname = "filname";
try {
URL url = new URL (urlreq);
String userPassword = username+ ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)uc.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content));
BufferedWriter ou = new BufferedWriter(new FileWriter("request.txt"));
String line;
int i = 1;
while ((line = in.readLine()) != null) {
System.out.println (line);
ou.write(line);
ou.newLine();
i++;
}
ou.close();
in.close();
} catch (MalformedURLException e) {
System.out.println ("Invalid URL");
} catch (IOException e) {
System.out.println ("Error reading URL");
}
return filname;
}

if you know java you should know whats going on here

Link to comment
Share on other sites

  • 3 years later...

I know this post is three years old, but this can be easier.... use an OO approach and download the Apache Commons HttpClient (I'm using the old 3.1 API for this). This way you can re-use the same HttpClient object.

This code will login, and return an InputStreamReader. Read the response, parse and store the token, and do go onto the rest of you're project. Think of HttpClient scriptable browser (kinda). It handles all the encoding, cookies, connecting, and other nut and bolts that I think is boring. All you'll have to worry about is parsing the response.

HttpClient client = new HttpClient();
Credentials cred = new UsernamePasswordCredentials("Username", "Password");
client.getState().setCredentials(AuthScope.ANY, cred);
GetMethod method = new GetMethod("http://localhost:9090/gui/token.html");
client.executeMethod(method);
InputStreamReader r = new InputStreamReader(get.getResponseBodyAsStream());

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...