Tutorial on developing webservice using URLConnection and axis1.4
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Log output to file using module: logging can generate a custom level log, and can output the log to a specified path. Log level: debug (debug log) = 5) {clearTimeout (time) // If all results obtained 10 consecutive times are empty Log clearing scheduled task}return}if(data.log_type==2){//If a new log is obtained for(i=0;i

Introduction to Caddy Caddy is a powerful and highly scalable web server that currently has 38K+ stars on Github. Caddy is written in Go language and can be used for static resource hosting and reverse proxy. Caddy has the following main features: Compared with the complex configuration of Nginx, its original Caddyfile configuration is very simple; it can dynamically modify the configuration through the AdminAPI it provides; it supports automated HTTPS configuration by default, and can automatically apply for HTTPS certificates and configure it; it can be expanded to data Tens of thousands of sites; can be executed anywhere with no additional dependencies; written in Go language, memory safety is more guaranteed. First of all, we install it directly in CentO

Using Jetty7 for Web Server Processing in JavaAPI Development With the development of the Internet, the Web server has become the core part of application development and is also the focus of many enterprises. In order to meet the growing business needs, many developers choose to use Jetty for web server development, and its flexibility and scalability are widely recognized. This article will introduce how to use Jetty7 in JavaAPI development for We

Face-blocking barrage means that a large number of barrages float by without blocking the person in the video, making it look like they are floating from behind the person. Machine learning has been popular for several years, but many people don’t know that these capabilities can also be run in browsers. This article introduces the practical optimization process in video barrages. At the end of the article, it lists some applicable scenarios for this solution, hoping to open it up. Some ideas. mediapipeDemo (https://google.github.io/mediapipe/) demonstrates the mainstream implementation principle of face-blocking barrage on-demand up upload. The server background calculation extracts the portrait area in the video screen, and converts it into svg storage while the client plays the video. Download svg from the server and combine it with barrage, portrait

First of all, you will have a doubt, what is frp? Simply put, frp is an intranet penetration tool. After configuring the client, you can access the intranet through the server. Now my server has used nginx as the website, and there is only one port 80. So what should I do if the FRP server also wants to use port 80? After querying, this can be achieved by using nginx's reverse proxy. To add: frps is the server, frpc is the client. Step 1: Modify the nginx.conf configuration file in the server and add the following parameters to http{} in nginx.conf, server{listen80

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.
