Showing posts with label Webservice. Show all posts
Showing posts with label Webservice. Show all posts

June 28, 2016

How to invoke HTTP methods like PUT,PATCH and DELETE using HttpURLConnection

     I am invoking REST web services using HttpURLConnection. Few of the rest services have operations like PUT, PATCH, and DELETE. While passing these methods in setRequestMethod() of HttpURLConnection object, I am getting the below error in response.

java.net.ProtocolException: Invalid HTTP method: PATCH

     To avoid this error and to let the HttpURLConnection execute these methods, there is a workaround which is to override the http method we are invoking. And the procedure to do it is,
  • Invoke any accepted http method like GET or POST
  • Pass the new method value to the header parameter "X-HTTP-Method-Override" to Override the method 
Below is the example: 
            URL url = new URL(mcsPatchAPIURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");

To get an idea about these methods:

GET: To get the details of a resource
PUT: To update the existing resource
POST: To create a new resource
PATCH: To update the existing resource partially
DELETE: To delete the existing resource

December 26, 2014

Sending Parameters to URL or web service in POST Method

Below code snippet explains how to send parameters to a web url in the POST method.
                               
URL url = new URL(“http://testulr/searchuser”);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoOutput(true);
String params=”userid=”+userid+”&username=”+name;
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(params);
writer.flush();
out.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder st = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
                st.append(line);
}
System.out.println("Result: "+st);
urlConnection.disconnect();
  
If the url is secured then replace HttpURLConnection class with HttpsURLConnection in the above code.

November 5, 2014

Error: oracle.jdevimpl.deploy.common.Jsr88RemoteDeployer

I am developing a REST web service. While deploying the web service to the application server, I got the below error.

Remote deployment failed (oracle.jdevimpl.deploy.common.Jsr88RemoteDeployer)

The error occurred because my web service has two methods with the same path name. After changing the path names, the issue got resolved and the web service was deployed successfully to the server. 

October 16, 2014

URL Encoding in Java

Whenever we are passing data to any url or web services, some characters of the data might be un recognised. Hence, it is always better to encode data before sending to URLs or web services, which we call as URL encoding. We have pre defined apis in java to do this encoding.

Below is an example to show how to encode a string in java.

String str="Hello World";
java.net.URLEncoder..encode(str, "UTF-8");

The output for this code would be:

Hello+World.

June 22, 2013

Changing End point address of web service


Below code explains how to change the web service end point through program. 

ServiceRequestService serviceRequestService = new ServiceRequestService();
ServiceRequestPortType serviceRequestPortType =
serviceRequestService.getServiceRequestPortType(); 
BindingProvider bp = (BindingProvider)serviceRequestPortType;           
bp.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:9950/soa-infra/services/default/sr_v2/ServiceRequest.service");

 Here is the explanation for this code.
  • Get port object of the web service
  • Type cast the port object to BindingProvider object 
  • BindingProvider class provides a member method getRequestContext() which returns a map
  • This map holds all the attributes of the request context
  • Overwrite end point with new web service url which will replace the new end point with the old one.
Note: This code works only if the client style is RPC.

June 21, 2013

Using HTTP Analyzer



JDeveloper provides a tool called “Http Analyzer “ which is very useful when we are using web service in our application and if we want to see what input request, the application is being sent to server via web service.

To start using the “Http Analyzer”, please do the below steps.

  • Go to Tools menu of JDeveloper and select Http Analyzer
  • Click on “Start HTTP Analyzer” button (Green button like arrow)
  • Start the Integrated web logic server and run the application
  • Do the necessary actions to invoke webservice integrated in the application
  • Now we can see request sent is logged in the HTTP Analyzer
  • Click on the request, it will open the XML request being sent to the server