Here i am writing a method which will how to implement the RestFull Web Services
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
String response = "";
public static String restfullResponse(){ // example method which return the Restfull webservice response
HttpClient client = new HttpClient(); //creating client to perform restfull webservices
String url = "URL"; // give the url to which you want to send the web services request
Credentials credentials = new UsernamePasswordCredentials("username","password");
client.getState().setCredentials(AuthScope.ANY, credentials);
client.getParams().setAuthenticationPreemptive(true);
// The above 3 lines are used for the authentication checking
PostMethod method = new PostMethod(url);// based upon requirement u can use post or get
method.addParameter("name", "inthiyaz");//to add any parameters to the URL
try {
int returnCode = client.executeMethod(method); //to execute the post or get method
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.out.println("The Post method is not implemented by this URL");
response = method.getResponseBodyAsString();
} else {
br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
while (((response = br.readLine()) != null)) {
return response;
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
String response = "";
public static String restfullResponse(){ // example method which return the Restfull webservice response
HttpClient client = new HttpClient(); //creating client to perform restfull webservices
String url = "URL"; // give the url to which you want to send the web services request
Credentials credentials = new UsernamePasswordCredentials("username","password");
client.getState().setCredentials(AuthScope.ANY, credentials);
client.getParams().setAuthenticationPreemptive(true);
// The above 3 lines are used for the authentication checking
PostMethod method = new PostMethod(url);// based upon requirement u can use post or get
method.addParameter("name", "inthiyaz");//to add any parameters to the URL
try {
int returnCode = client.executeMethod(method); //to execute the post or get method
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.out.println("The Post method is not implemented by this URL");
response = method.getResponseBodyAsString();
} else {
br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
while (((response = br.readLine()) != null)) {
return response;
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
No comments:
Post a Comment