Hi, You can use the new version of Apache's HTTP library, as follows, hope it'll help you.
public class RestTest { /** * Create and initialize the http client. * * @return an HttpClient object */ HttpClient getClient(host, port, username, 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(yourHost, yourPort, yourUsername, yourPassword); GetMethod getMethod = new GetMethod(getURL); getMethod.addRequestHeader("Accept", "application/json"); getMethod.setDoAuthentication( true ); String getURL = "your get url"; 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 = "your put url"; 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(); } } } Best regards, Mohamed. On Sun, Apr 17, 2016 at 9:24 PM, Mohamed Boussaa <mohamed.bous...@xwiki.com> wrote: > Hi, > > You can use the new version of Apache's HTTP library, as follows, hope > it'll help you. > > public class RestTest { > /** > * Create and initialize the http client. > * > * @return an HttpClient object > */ > HttpClient getClient(host, port, username, 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(yourHost, yourPort, yourUsername, > yourPassword); > GetMethod getMethod = new GetMethod(getURL); > getMethod.addRequestHeader("Accept", "application/json"); > getMethod.setDoAuthentication( true ); > String getURL = "your get url"; > 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 = "your put url"; > 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(); > } > } > } > > > > > Best regards, > Mohamed. > > On Sun, Apr 17, 2016 at 7:25 PM, Tobi <gman...@yandex.ru> wrote: > >> Hello, >> >> I was trying to write a program which would upload new pages on my wiki. I >> was following these instructions >> http://platform.xwiki.org/xwiki/bin/view/Features/XWikiRESTfulAPI , but >> unfortunately most of the examples use old version of Apache's HTTP >> library, >> so they are not very useful for me. >> >> So I decided to try writing a program by following Apache's examples, but >> when I was testing connectivity I realised that I can't even get access to >> admin-only pages. >> >> Here's my code: >> >> import java.io.IOException; >> import org.apache.http.auth.AuthScope; >> import org.apache.http.auth.UsernamePasswordCredentials; >> import org.apache.http.client.CredentialsProvider; >> import org.apache.http.client.methods.CloseableHttpResponse; >> import org.apache.http.client.methods.HttpGet; >> import org.apache.http.impl.client.BasicCredentialsProvider; >> import org.apache.http.impl.client.CloseableHttpClient; >> import org.apache.http.impl.client.HttpClients; >> import org.apache.http.util.EntityUtils; >> >> public class RestTest { >> >> public static void main(String[] args) throws IOException { >> CredentialsProvider credsProvider = new >> BasicCredentialsProvider(); >> credsProvider.setCredentials( >> new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, >> AuthScope.ANY_REALM, AuthScope.ANY_SCHEME), >> new UsernamePasswordCredentials("Tobi", "mypass")); >> CloseableHttpClient httpclient = HttpClients.custom() >> .setDefaultCredentialsProvider(credsProvider) >> .build(); >> try { >> HttpGet httpget = new >> HttpGet("http://xwiki.aircr.ru/xwiki/bin/edit/Main/WebHome"); >> >> System.out.println("Executing request " + >> httpget.getRequestLine()); >> CloseableHttpResponse response = httpclient.execute(httpget); >> try { >> >> System.out.println("----------------------------------------"); >> System.out.println(response.getStatusLine()); >> EntityUtils.consume(response.getEntity()); >> } finally { >> response.close(); >> } >> } finally { >> httpclient.close(); >> } >> } >> >> } >> >> It gives me: >> Executing request GET http://xwiki.aircr.ru/xwiki/bin/edit/Main/WebHome >> HTTP/1.1 <http://xwiki.aircr.ru/xwiki/bin/edit/Main/WebHomeHTTP/1.1> >> ---------------------------------------- >> HTTP/1.1 401 Unauthorized >> >> >> I would really appreciate any help! Also, it would be great if you show me >> more examples of using PUT method for xWiki RESTful! >> >> >> >> -- >> View this message in context: >> http://xwiki.475771.n2.nabble.com/xWiki-RESTful-HTTP-Connection-tp7599026.html >> Sent from the XWiki- Users mailing list archive at Nabble.com. >> _______________________________________________ >> users mailing list >> users@xwiki.org >> http://lists.xwiki.org/mailman/listinfo/users >> > > _______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users