Write in front:
There are many ways to call webservice: 1. Use tools directly on the client to generate client code and convert the code Just copy it into the project and call it; 2. Use the corresponding webservice framework to make the call. For example, if our server-side development uses axis, then I can also import the corresponding axis jar package on the client side, and then use it Related methods to call; 3. js call; 4. URLConnection call. Personally, I think the first two methods above are more suitable when both the server and the client are Java development systems. If they are called in different languages, it is difficult to say. The third and fourth methods are actually similar. If you need to call the interface in a jsp page, use js. If you need to write a program in the background to call it, URLConnection is more recommended (you can only use it now, In fact, I am still very vague about some of its principles. For example, should this method be called by http??? It is not very clear yet)
## Recently, the project needs to develop a webservice interface. But jdk is 1.4. Therefore, axis1.4 was chosen for development (only this is more suitable for the project environment).
Here I also use the java language for testing, which can be called. To parse the data returned by the server, you need to use the relevant jar package:
package edu.hue.client;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.StringReader;import java.net.HttpURLConnection;import java.net.ProtocolException;import java.net.URL;import java.net.URLConnection;import java.util.List;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class XMLClient2 {public static void main(String[] args) {try {//创建url地址URL url = new URL("http://10.203.138.82:8080/test_axis3/services/sayHello?wsdl");//打开连接URLConnection conn = url.openConnection();//转换成HttpURLHttpURLConnection httpConn = (HttpURLConnection) conn; System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //打开输入输出的开关httpConn.setDoInput(true); httpConn.setDoOutput(true);//post提交不能有缓存httpConn.setUseCaches(false); //设置请求方式httpConn.setRequestMethod("POST"); //设置请求的头信息httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8"); //设置 SOAPAction Header 不然 报错 没有这个soapaction headerhttpConn.setRequestProperty("SOAPAction", ""); //拼接请求消息 这里的请求消息体 直接用接口测试工具 soapui 来获取 然后拼接以下 注意双引号这里要转义成\" String data = "<soapenv:Envelope xmlns:soapenv=" + "\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:ser=\"http://server.hue.edu/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"//+"<soapenv:Header />" +"<soapenv:Body>" +"<ser:say soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +"<name xsi:type=\"soapenc:string\" xs:type=\"type:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xs=\"http://www.w3.org/2000/XMLSchema-instance\">" +"小蚊子qq:513996980" //这里是直接在soapui中复制过来的所以它的请求消息体比较长 也可以用下面这种 注释的方式来拼接+"</name>" +"</ser:say>" +"</soapenv:Body>" +"</soapenv:Envelope>"; /* 下面这种请求消息体更为简单 经过测试也可以成功 它的方法名 参数名 都很简洁 * 但是为了保险 希望大家在写请求消息体的时候用 接口测试工具去获取比如soapui 然后直接复制过来 String data = "<soapenv:Envelope xmlns:soapenv=" + "\"\" " + "xmlns:ser=\"http://server.hue.edu/\" " + "xmlns:xsd=\"\" " + "xmlns:xsi=\"\">" +"<soapenv:Header />" +"<soapenv:Body>" +"<ser:say >" +"<name>小蚊子qq:513996980</name>" +"</ser:say>" +"</soapenv:Body>" +"</soapenv:Envelope>";*/ //获得输出流OutputStream out = httpConn.getOutputStream();//发送数据 这里注意要带上编码utf-8 不然 不能传递中文参数过去out.write(data.getBytes("UTF-8"));//判断请求成功if(httpConn.getResponseCode() == 200){ System.out.println("调用成功.....");//获得输入流InputStream in = httpConn.getInputStream();//使用输入流的缓冲区BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); StringBuffer sb = new StringBuffer(); String line = null;//读取输入流while((line = reader.readLine()) != null){ sb.append(line); } //创建sax的读取器 这里需要导入相应的jar包SAXReader saxReader = new SAXReader();//创建文档对象Document doc = saxReader.read(new StringReader(sb.toString()));//获得请求响应return元素 这里可根据接口测试工具查看你的相应消息体的返回值的节点是什么名称 我这里是sayReturnList eles = doc.selectNodes("//sayReturn"); for(int i=0;i<eles.size();i++){ Element ele = (Element)eles.get(i); System.out.println(ele.getText()); } System.out.println(sb.toString()); }else{ //调用不成功 打印错误的信息 //获得输入流InputStream err = httpConn.getErrorStream();//使用输入流的缓冲区BufferedReader reader = new BufferedReader(new InputStreamReader(err,"UTF-8")); StringBuffer sb = new StringBuffer(); String line = null;//读取输入流while((line = reader.readLine()) != null){ sb.append(line); } System.out.println("返回错误码:"+httpConn.getResponseCode()); System.out.println("返回的结果:"+sb.toString()); } } catch (Exception e) { e.printStackTrace(); } } }
Summary of the problem:
1. Report an error saying http response result code 500: java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.203.138.82:8080/test_axis/services/sayHello
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection .java:1626)2. Bug for axis1.4: AxisFault faultCode: {}Client.NoSOAPAction faultSubcode: faultString: no SOAPAction header! faultActor: faultNode: faultDetail: {}stackTrace:no SOAPAction header!
No SOAPAction header! Here you can note that if you report this error and add a line of code, it will be fine. I read almost a day or two of blog posts about this error, but no one told me the specific code solution. (There are two solutions: 1: Add soapaction on the client. Its specific content is not important. It is also possible to write an empty string. 2: The server rewrites a servlet, the same as axisServlet, and rewrites the getSoapAction inside. () method, only the first method is recommended here, after all, this is not a bug of axis)
//Set SOAPAction Header Otherwise, the error will not be reported without this soapaction header
httpConn.setRequestProperty("SOAPAction", "");
3. If the parameter passed by the client is in Chinese, an error will be reported: when it becomes a byte array, add UTF-8
//Get the output stream
OutputStream out = httpConn.getOutputStream(); //Send data Be careful to bring the encoding utf-8 here, otherwise you cannot pass Chinese parameters.
out.write(data.getBytes("UTF-8"));
Question: How to call a method if it has no parameters? ? ? Here, when calling the .net system developed by WCF across platforms before, the interface method called did not need to pass parameters, but an empty string was passed. There was no soap request message body or anything like that. But here because It is a webservice, it is based on soap, so whether there are parameters or not when passing data here, the message request body should be written, so you need to pay attention here
For example:
// 但是为了保险 希望大家在写请求消息体的时候用 接口测试工具去获取比如soapui 然后直接复制过来 String data = "<soapenv:Envelope xmlns:soapenv=" + "\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:ser=\"http://server.hue.edu/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +"<soapenv:Header />" +"<soapenv:Body>" +"<ser:say >"//比如这里要调用的方法没有参数 就直接不用写就好 但是这个消息体 应该还是要的 +"</ser:say>" +"</soapenv:Body>" +"</soapenv:Envelope>";
The above is the detailed content of Tutorial on developing webservice using URLConnection and axis1.4. For more information, please follow other related articles on the PHP Chinese website!