That's because what Mohamed is referring is the old (3.x) HttpClient
and not the new (4.x) one which have different (sometime very
different) API.
The main issue you have is probably the fact that XWiki does not ask
the client for credential by default since anonymous access is valid.
That means you need to force preemptive authentication which is very
easy in 3.x (just a call to setAuthenticationPreemptive like in
Mohamed's example) and quite harder in 4.x (they find dangerous to
support it like they used to...).
You can find various examples on Google and the official documentation
is on
(section "4.6. Preemptive authentication").
On Mon, Apr 18, 2016 at 3:05 PM, Tobi <gman007(a)yandex.ru> wrote:
For some reasons it gives me this
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:65)
at RestTest2.getClient(RestTest2.java:16)
at RestTest2.main(RestTest2.java:27)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Code:
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.xwiki.rest.model.jaxb.ObjectFactory;
import org.xwiki.rest.model.jaxb.Page;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.NameValuePair;
public class RestTest2 {
static HttpClient getClient(String host, int port, String username, String
password) {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setAuthenticationPreemptive(true);
httpClient.getState().setCredentials(
new AuthScope(host, port, AuthScope.ANY_REALM),
new UsernamePasswordCredentials(username, password)
);
return httpClient;
}
public static void main(String[] args) throws IOException {
// Get request example
HttpClient httpClient = getClient("xwiki.aircr.ru", 8080,
"Tobi",
"gaben1337");
GetMethod getMethod = new
GetMethod("http://xwiki.aircr.ru/xwiki/bin/view/Main/");
getMethod.addRequestHeader("Accept", "application/json");
getMethod.setDoAuthentication( true );
String getURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";
try {
int status = httpClient.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
} catch (Exception e) {
//...
} finally {
// release any connection resources used by the method
getMethod.releaseConnection();
}
// Put request example
String putURL = "http://xwiki.aircr.ru/xwiki/bin/view/Main/";
PutMethod putMethod = new PutMethod(putURL);
putMethod.addRequestHeader("Accept",
"application/json");
putMethod.setDoAuthentication( true );
try {
int status = httpClient.executeMethod(putMethod);
System.out.println(putMethod.getResponseBodyAsString());
} catch (Exception e) {
// ...
} finally {
// release any connection resources used by the method
putMethod.releaseConnection();
}
}
}
--
View this message in context:
http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026p7…
Sent from the XWiki- Users mailing list archive at
Nabble.com.
_______________________________________________
users mailing list
users(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/users