|
From: Bill B. <bb...@re...> - 2013-10-14 19:13:51
|
Maybe because your REST service requires authentication? On 10/14/2013 2:54 PM, Nuwan Bandara wrote: > Can someone help me on this? > > Thanks. > > > On Fri, Oct 11, 2013 at 1:15 PM, Nuwan Bandara <mai...@gm... > <mailto:mai...@gm...>> wrote: > > Hi, > > I use the following code to call a web service using POST and always > get "POST method always returns "Error status 401 Unauthorized"" > > Can someone help me to figure out what I do wrong in the bellow code? > > Thanks in advance, > Nuwan > > > *FYI: webServiceUrl: > http://apitest.collabrx.com/oncovar/V1-0-7?client_id=XXX&command=submit_sample* > > public Document post(String webServiceUrl, String data) throws > ExternalResourceConnectorException { > logger.debug("Posting data to URL: " + webServiceUrl); > SAXReader saxReader = new SAXReader(); > Document document = null; > StringBuffer responseMsg = new StringBuffer(); > try { > ClientRequest request = new ClientRequest(webServiceUrl); > request.accept("application/xml"); > request.body(MediaType.APPLICATION_XML, data); // data > to be identified. > ClientResponse<String> response = > request.post(String.class); > //Object response = request.post().getEntity(new > GenericType<Object>(){}); > if (response.getStatus() != 200) { > throw new RuntimeException("Failed : HTTP error > code : " + response.getStatus()); > } > BufferedReader br = new BufferedReader(new > InputStreamReader(new ByteArrayInputStream(response.getEntity() > .getBytes()))); > String output; > while ((output = br.readLine()) != null) { > responseMsg.append(output); > } > logger.info <http://logger.info>("External web service response: "+ > responseMsg.toString()); > document = saxReader.read(new > StringReader(responseMsg.toString())); > } > catch (ClientProtocolException e) { > throw new ExternalResourceConnectorException(e); > } > catch (IOException e) { > throw new ExternalResourceConnectorException(e); > } > catch (Exception e) { > throw new ExternalResourceConnectorException(e); > } > return document; > } > > > > > ------------------------------------------------------------------------------ > October Webinars: Code for Performance > Free Intel webinars can help you accelerate application performance. > Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from > the latest Intel processors and coprocessors. See abstracts and register > > http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk > > > > _______________________________________________ > Resteasy-users mailing list > Res...@li... > https://lists.sourceforge.net/lists/listinfo/resteasy-users > -- Bill Burke JBoss, a division of Red Hat http://bill.burkecentral.com |