Advertisement
vamsiampolu

403 error when calling webservice

May 15th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. I am trying to call a web-service from my servlet,the web-service interacts with the database and in this case I am trying to insert into the database,however I get a strange 403 status code in the response although I have not designated it so.
  2.  
  3. This is me calling the web-service:
  4.  
  5.     public static void makePutRequest(String objtype,String objkey,String json)
  6. {
  7.     String uri=BASE_URI+objtype+"/"+objkey;
  8.     System.out.println("Making a put request to the url "+uri);
  9.     HttpURLConnection conn=null;
  10.     URL url=null;
  11.     BufferedWriter writer=null;
  12.     try {
  13.         url=new URL(uri);  
  14.     } catch (MalformedURLException e) {
  15.         e.printStackTrace();
  16.     }
  17.     try {
  18.         conn=(HttpURLConnection) url.openConnection();
  19.         conn.addRequestProperty("Content-Type", "application/json");
  20.         conn.setRequestMethod("PUT");
  21.         conn.setDoInput(true);
  22.         conn.setDoOutput(true);
  23.         System.out.println(json);
  24.         writer=new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
  25.         writer.write(json);
  26.         writer.flush();
  27.         int rc=conn.getResponseCode();
  28.         System.out.println("The response code is "+rc);
  29.     } catch (IOException e) {
  30.         e.printStackTrace();
  31.     }
  32.     finally
  33.     {
  34.         if(writer!=null)
  35.         {
  36.             try {
  37.                 writer.close();
  38.             } catch (IOException e) {
  39.                 e.printStackTrace();
  40.             }
  41.             if(conn!=null)
  42.                 conn.disconnect();
  43.         }
  44.     }
  45. }
  46. This is the response in the console:
  47.  
  48.  Making a put request to the url  http://localhost:8180/GoogleMapsErpProject/rest/GoogleMapsErp/EQUI/242423
  49.  The response code is 403
  50. I have found that the web-service actually never gets called which might mean that my url is wrong,so I tried calling it with this:
  51.  
  52.    http://localhost:8180/rest/GoogleMapsErp/EQUI/242423
  53. GoogleMapErpProject is the name of my project.This is my web.xml file:
  54.  
  55.    <web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  56.   <display-name></display-name>
  57.   <servlet>
  58. <servlet-name>Jersey REST Services</servlet-name>
  59. <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  60. <init-param>
  61.   <param-name>com.sun.jersey.config.property.packages</param-name>
  62.   <param-value>com.hastha.maperp.webservice</param-value>
  63. </init-param>
  64. <load-on-startup>1</load-on-startup>
  65. </servlet>
  66.  <servlet-mapping>
  67. <servlet-name>Jersey REST Services</servlet-name>
  68.  <url-pattern>/rest/*</url-pattern>
  69.  </servlet-mapping>
  70. I have tried to make a cURL request to the web-service:
  71.  
  72.  curl -i -X PUT -H "Content-Type:application/json" -d  
  73.  '{"gisuniqkey":"2434fd22dv3","geometry":{"type":"Point","coordinates":  
  74.   [-17.895114303749143,150.908203125]}}'  
  75.   http://localhost:8180/GoogleMapsErpProject/rest/GoogleMapsErp/EQUI/123123
  76.   HTTP/1.1 403 Forbidden
  77.   Server: Apache-Coyote/1.1
  78.   Content-Type: text/html;charset=utf-8
  79.   Content-Length: 961
  80.   Date: Thu, 15 May 2014 11:28:07 GMT
  81.  
  82.   curl -i -X PUT -H "Content-Type:application/son" -d  
  83.   '{"gisuniqkey":"2434fd22dv3","geometry":{"type":"Point","coordinates":
  84.   [-17.895114303749143,150.908203125]}}'  
  85.   http://localhost:8180/rest/GoogleMapsErp/EQUI/123123
  86.   HTTP/1.1 403 Forbidden
  87.   Server: Apache-Coyote/1.1
  88.   Content-Type: text/html;charset=utf-8
  89.   Content-Length: 961
  90.   Date: Thu, 15 May 2014 11:30:54 GMT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement