webservice调用方式:
1.httpget
2.httpost
3.httpsoap
其中soap的优点是可以传递结构化的数据,前两者不行。
webservice的通讯原理是其底层也是通过http传输的。所以手动连接webservice server有两种方式。
第一种是使用jaxm
参考如下:
http://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html
这种方法比较复杂,使用soapconnection
public static void post2WebService(String content, String url) { try { // First create the connection SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory .newInstance(); SOAPConnection connection = soapConnFactory.createConnection(); // Next, create the actual message MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage message = messageFactory.createMessage(); // Create objects for the message parts SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); // String to inputstream InputStream inputstream = new ByteArrayInputStream( content.getBytes("UTF-8")); // Populate the Message StreamSource preppedMsgSrc = new StreamSource(inputstream); soapPart.setContent(preppedMsgSrc); // Save the message message.saveChanges(); // Send the message and get a reply // Set the destination URLEndpoint destination = new URLEndpoint(url); // Send the message SOAPMessage reply = connection.call(message, destination); // Create the transformer TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Extract the content of the reply Source sourceContent = reply.getSOAPPart().getContent(); // Set the output for the transformation StringWriter writer = new StringWriter(); // StreamResult result = new StreamResult(System.out); StreamResult result = new StreamResult(writer); transformer.transform(sourceContent, result); System.out.println("result : " + writer.toString()); // Close the connection connection.close(); } catch (Exception e) { LOG.error("post2WebService error : " + e.getMessage()); } }
此处是一小段代码参考,如何将streamResult转换为String的
try { StreamSource source = new StreamSource(new StringReader("<xml>blabla</xml>")); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(source,result); String strResult = writer.toString(); } catch (Exception e) { e.printStackTrace(); }
这里还需要使用到jaxm-api.jar
第一种方法较为复杂,现在介绍第二种方法,直接使用httpConnection post message
public static String sendPost2WS(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); //open url connect URLConnection conn = realUrl.openConnection(); //set common properties conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); //send POST request must set 2 lins below conn.setDoOutput(true); conn.setDoInput(true); //get URLConnection object out put stream out = new PrintWriter(conn.getOutputStream()); //send request parameters out.print(param); // flush out put stream cache out.flush(); //define BufferedReader to get the URL response in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("send POST has error : "+e.getMessage()); e.printStackTrace(); } //close in & out put stream finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; }
相关推荐
wince系统连接webService c# 源码
使用axis手动编写webservice服务发布,并调用。 使用wsdl2java命令 ---------注意导入axis jar包
Android连接webservice测试代码
连接webservice的示例,两示例:dateset连接.net,调用java返回String
手动加载WebService服务端应用程序,手动加载WebService服务端应用程序
DashBoard连接Webservice Demo
Qt自带的连接webservices方法,qtsoap,使用很方便。
最近在开发j2me,需要连接Webservice,可是在ksoap官网下载的包用不了,想到这个是开源包,应该有源代码,在不同的网站上找了很久,终于收集到了MIDP设备连接Webservice的源代码。经过我的使用,证明是可以用的。...
vc++连接webservice soap协议
Android 连接 Webservice 的 demo演示项目源代码文件。
android通过webservice连接sqlserver soap协议,接口连接 数据获取
连接webservice数据接口,获取数据的小例子,这里使用的是winfrom来展示,主要内容是怎么获取webservice的数据,同时怎么解析。
详细描述怎么与webservice建立私密连接
struts2+spring连接webservice jar包
cxf技术连接webservice接口所需所有jar包,我自己做项目时自己用的。
Android通过webservice连接SqlServer实例(Android4.0可运行)从原博http://blog.csdn.net/zhyl8157121/article/details/8169172下载下来的demo,看了评论和其它文章后修改,解决了Android2.3以上StrictMode模式下不...
android 用webservice连接后台数据库
android连接webservicejar 非常好用
android端通过ksoap-android库连接ASP.NET的WebService的最简代码