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
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 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 {
>>