JAVA :: RESTful Web服务使用XML文件

编程入门 行业动态 更新时间:2024-10-28 13:15:49
本文介绍了JAVA :: RESTful Web服务使用XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

除了以FORMPARAM格式外,还有其他方法可以将XML文件发送到RESTful Web服务吗?

Is there any other way that we can send an XML file to a RESTful Web Service other than as a FORMPARAM?

我的要求是开发一个使用XML文件的Web服务,将其存储在我的本地计算机中,并返回一条声明该文件已下载/保存的声明.

My requirement is to develop a webservice which Consumes a XML file, stores it in my local machine and returns a statement saying that the file was downloaded/saved.

推荐答案

这里是要发布的代码,比SOAP更容易...

Here's the code to post, way easier than SOAP...

// POST the XML string as text/xml via HTTPS public static String postRequest(String strRequest, String strURL) throws Exception { String responseXML = null; try { URL url = new URL(strURL); URLConnection connection = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) connection; byte[] requestXML = strRequest.getBytes(); // Set the appropriate HTTP parameters. httpConn.setRequestProperty("Content-Length", String.valueOf(requestXML.length)); httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); // Send the String that was read into postByte. OutputStream out = httpConn.getOutputStream(); out.write(requestXML); out.close(); // Read the response and write it to standard out. InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); BufferedReader br = new BufferedReader(isr); String temp; String tempResponse = ""; //Create a string using response from web services while ((temp = br.readLine()) != null) tempResponse = tempResponse + temp; responseXML = tempResponse; br.close(); isr.close(); } catch (java.MalformedURLException e) { System.out.println("Error in postRequest(): Secure Service Required"); } catch (Exception e) { System.out.println("Error in postRequest(): " + e.getMessage()); } return responseXML; }

更多推荐

JAVA :: RESTful Web服务使用XML文件

本文发布于:2023-10-09 21:05:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   RESTful   JAVA   XML   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!