cancel
Showing results for 
Search instead for 
Did you mean: 

Web script accessing through Apache HTTP

mt44555
Champ in-the-making
Champ in-the-making
hi all,

I have created a web script and tested it sucessfully in mozilla. now i want to access this through Apahche HTTPclient model. i am getting login ticket and parsing the xml and getting ticket value . I have taken the web script url and appeded the ticket value. used the following code

HttpClient client = new HttpClient();
      GetMethod method = new GetMethod(contenturl.toString());
      Credentials defaultcreds = new UsernamePasswordCredentials("tomcat",
            "tomcat");
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, AuthScope.ANY_REALM),
            defaultcreds);
      client.getParams().setAuthenticationPreemptive(true);
      // client = new URL(contenturl.toString());
[b]in try block[/b]
int responseCode = client.executeMethod(method);
InputStream resultdoc = method.getResponseBodyAsStream();


The above HTTP POST MEthod, i have used GET Method too..

but getting the 401 error
3 REPLIES 3

mt44555
Champ in-the-making
Champ in-the-making
hi all,

I have created a web script and tested it sucessfully in mozilla. now i want to access this through Apahche HTTPclient model. i am getting login ticket and parsing the xml and getting ticket value . I have taken the web script url and appeded the ticket value. used the following code

HttpClient client = new HttpClient();
      GetMethod method = new GetMethod(contenturl.toString());
      Credentials defaultcreds = new UsernamePasswordCredentials("tomcat",
            "tomcat");
      client.getState().setCredentials(
            new AuthScope("localhost", 8080, AuthScope.ANY_REALM),
            defaultcreds);
      client.getParams().setAuthenticationPreemptive(true);
      // client = new URL(contenturl.toString());
[b]in try block[/b]
int responseCode = client.executeMethod(method);
InputStream resultdoc = method.getResponseBodyAsStream();


The above HTTP POST MEthod, i have used GET Method too..

but getting the 401 error
please help me… Smiley Tongue

openpj
Elite Collaborator
Elite Collaborator
You can try using this code to implement authentication for POST method:

HttpClient httpClientAuth = new HttpClient();
httpClientAuth.getParams().setAuthenticationPreemptive(true);
HostConfiguration host = httpClientAuth.getHostConfiguration();
AuthScope authScope = new AuthScope(host.getHost(), host.getPort());
HttpState state = httpClientAuth.getState();
Credentials defaultcreds = new UsernamePasswordCredentials(usernameValue, passwordValue);
state.setCredentials(authScope, defaultcreds);
PostMethod post = new PostMethod(authUrl);
post.setDoAuthentication(true);
post.addParameter(usernameParameter, usernameValue);
post.addParameter(passwordParameter, passwordValue);
….
Hope this helps.

arpit_gupta
Champ in-the-making
Champ in-the-making
can you post your get URL?

I have also used Apache HTTP client for accessing alfresco webscripts and in my case for a GET request appending ticket like   alf_ticket=TICKET_49c5a98872fc2cbe2df8312001077dcf5ca8e14c works fine.

also there is no need to add authentication credentials when you are appending a ticket in your GET request.

try this


String url = yoururl+"?alf_ticket="+ticket
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity myEntity = response.getEntity();
String responseString = EntityUtils.toString(myEntity);

Thanks,
Arpit
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.