bszabolcs: If you've got a code snippet which works, would you be willing
to paste it in this ticket, so we can add it to the Wiki, and amend the
code to incorporate the calls that you made?
It's really simple. Do something like this:
this.rpcclient = new XMLRPCClient(buildWebUIUrl(),
settings.getSslTrustAll());
if (settings.shouldUseAuthentication()) {
this.rpcclient.setBasicAuthentication(settings.getUsername(),
settings.getPassword());
}
this.rpcclient.call("load_start", new String[] { url });
This is taken from my project. Full source code:
http://code.google.com/p/transdroid/source/browse/trunk/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java
To do pre-emptive authorization, look for the line 'HttpResponse response =
client.execute(postMethod);' in the XMLRPCClient.java file and add this
just before the execute():
// Force preemptive authentication
// This makes sure there is an 'Authentication: ' header being send before
trying and failing and retrying
// by the basic authentication mechanism of DefaultHttpClient
postMethod.addHeader("Authorization", "Basic " +
Base64.encodeBytes((username + ":" + password).getBytes()));
This manually adds the authorization header. Hope this helps.
I have defined this innerclass in XMLRPCClient.java:
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext
context) throws HttpException, IOException {
AuthState authState = (AuthState)
context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)
context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(),
targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
};
and I have added the first line to activate preemptive authentication:
((DefaultHttpClient) client).addRequestInterceptor(preemptiveAuth, 0);
((DefaultHttpClient) client).getCredentialsProvider().setCredentials(
new AuthScope(postMethod.getURI().getHost(),
postMethod.getURI().getPort(),AuthScope.ANY_REALM),
new UsernamePasswordCredentials(username, password));
Comment #28 on issue 6 by j...@sprig.gs: HTTP Basic authentication &
Pre-emptive Authentication
http://code.google.com/p/android-xmlrpc/issues/detail?id=6
Thanks for these code snippets erickok and bszabolcs. I'll try and
implement something around this over the next week or so.
The Code snippets are working very well. I implement them because i need
them for my project. If you tell me how I can push them, I will do that
tomorrow ;)
Attachments:
XMLRPCClient.java 17.3 KB
j.rehborn thanks for your code - it's best to attach patches rather than
complete files, as it helps us to work out what the differences are that
you've introduced.
I copied your code into the tree, and it introduced an error (around the
Base64 library), so to be honest, as I'd already started on this, I've not
looked too deeply into fixing the issue in your code, or looking at whether
it's a clearer and cleaner piece of code. I'll be pushing the patch
inspired by erickok's code snippet, shortly (as part of a few patches), but
in the meantime, here's the patch I'll be pushing.
(Note, I've just spotted that I've not credited erickok as inspiration,
this will be fixed in the push.)
Attachments:
issue6.patch 2.8 KB
Comment #31 on issue 6 by j...@sprig.gs: HTTP Basic authentication &
Pre-emptive Authentication
http://code.google.com/p/android-xmlrpc/issues/detail?id=6
This code has been committed, and requires testing.
Hi canyumusak,
I'm not certain whether the issue you've mentioned is linked to this issue
- can you advise what happens when you try and run your script? Can you
provide any log output? Do you see the access attempt occur on your web
server? Are you able to provide any packet capture of what happens when you
try to make your connection?
Also, are you able to try the following:
try {
serverURL = new URL(server+":"+port);
} catch (MalformedURLException e) {
System.out.println("Invalid URL - could not connect!");
}
myServer = new XMLRPCClient(serverURL, "myuser", "mypw");
state = (HashMap<String,Integer>) myServer.call("Tester.getStatement",
params );
The current version of the code, while it's not covered in the
documentation, does support client creation method.
Hey jon,
i am currently running the same client in a normal JRE, using the XML-RPC
Client creation of apache.
I tried several combinations (myServer = new
XMLRPCClient(serverURL, "myuser", "mypw"), myServer = new XMLRPCClient(new
URL(http://myuser:my...@192.168.2.24/));, etc..).
My Server can't seem to recognize the username and password.
I tried to look at the logcat log now. It returns me the attached error (i
think it's the fatal one). Should i continue posting here?
greetings,
Geki
Attachments:
Logcat.txt 4.4 KB
I tried several combinations.
I attached my Logcat results. I suppose, that this XML-RPC Client is not
compatible with the apache XML-RPC Server.
What XML-RPC java alternative should I use?
greetings,
Geki
Attachments:
Logcat.txt 7.9 KB
What i forgot to write: The Server gives me this error:
28.12.2010 01:15:25 org.apache.xmlrpc.server.XmlRpcErrorLogger log
SCHWERWIEGEND: Not authorized
org.apache.xmlrpc.common.XmlRpcNotAuthorizedException: Not authorized
at
org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.execute(ReflectiveXmlRpcHandler.java:84)
at
org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:46)
at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:86)
at
org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:200)
at org.apache.xmlrpc.webserver.Connection.run(Connection.java:208)
at org.apache.xmlrpc.util.ThreadPool$Poolable$1.run(ThreadPool.java:68)
28.12.2010 01:22:21 org.apache.xmlrpc.server.XmlRpcErrorLogger log
Certainly this library can't handle URLs in the format
http://username:password@hostname/path/to/xmlrpc.svc
but it should be able to cope with
http://hostname/path/to/xmlrpc.svc
although I also noticed in your URL creation above (in comment 32), you've
used
serverURL = new URL(server+":"+port);
Is there any reason you're quoting port numbers? Given our inflexibility in
the authentication portion, it would probably be worth trying (if you're
using a different port) running the service on straight port 80, and trying
just using the straight URL (no ports or auth details)?
Thus your code would be:
myServer = new XMLRPCClient(server_url, "user", "passwd");
I'm not saying this will fix it, but it's something else to try?
I get absolutely the same error.
Trying to get it to work "anyhow" - i set the auth-method to always return
true. It works this way.
My port was 8080, changing it to 80 didn't change the error or the way it
behaves.
Unfortunately i am programming this with access to critical data, hence i
can't leave it without any authentication.
I am thinking about migrating away from apache xml-rpc in java, if this
solves the error. Are there good alternatives?
News:
Using myServer = new XMLRPCClient(server_url, "user", "passwd"); ends in
the error i wrote above.
myServer = new XMLRPCClient(server_url); connects succesfully (with no Auth
requested)
first of all thanks for the code, then:
still bad user name and password issue for wordpress?
Comment #41 on issue 6 by j...@sprig.gs: HTTP Basic authentication &
Pre-emptive Authentication
http://code.google.com/p/android-xmlrpc/issues/detail?id=6
I'm no longer able to commit time to this project, and as such, I am
removing myself from any tickets I've previously been involved in.