Ludjer Posted March 28, 2007 Report Share Posted March 28, 2007 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 passwordCODE 410 = HTTP Error 401 Unauthorized Explainedhere is my codeimport 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 More sharing options...
Lord Alderaan Posted March 28, 2007 Report Share Posted March 28, 2007 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 loginstringWhere 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 More sharing options...
Ludjer Posted March 28, 2007 Author Report Share Posted March 28, 2007 mmm ok thx for your help. and now to google and java books==---EDIT---== after talking to a guy on mirc he gave me this site http://www.javaworld.com/javaworld/javatips/jw-javatip47.htmlit has exatly how to connect and how to use the URL authenticationanyway if anyone ever needs help don't be afraid to ask me Link to comment Share on other sites More sharing options...
Lord Alderaan Posted March 29, 2007 Report Share Posted March 29, 2007 Nice link indeed. Exactly what people would need to get authentication on webui to work with java.Any programming language that allows you to base64 encode and manually add to the request header will allow you to query the webui. Link to comment Share on other sites More sharing options...
Ludjer Posted April 2, 2007 Author Report Share Posted April 2, 2007 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 More sharing options...
ticktock Posted June 6, 2010 Report Share Posted June 6, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.