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 = "<envelope>"//+"<header></header>" +"<body>" +"<say>" +"<name>" +"小蚊子qq:513996980" //这里是直接在soapui中复制过来的所以它的请求消息体比较长 也可以用下面这种 注释的方式来拼接+"</name>" +"</say>" +"</body>" +"</envelope>"; /* 下面这种请求消息体更为简单 经过测试也可以成功 它的方法名 参数名 都很简洁 * 但是为了保险 希望大家在写请求消息体的时候用 接口测试工具去获取比如soapui 然后直接复制过来 String data = "<envelope>" +"<header></header>" +"<body>" +"<say>" +"<name>小蚊子qq:513996980</name>" +"</say>" +"</body>" +"</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><div class="cnblogs_code"> The above is the code called by the client. I won’t post more on the server here. It’s just a method with a String type parameter. </div> <p> </p> <p>Summary of the problem:<span style="font-size: 16px"><strong></strong></span></p> <p>1. Report an error saying http response result code 500:<strong></strong> java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.203.138.82:8080/test_axis/services/sayHello</p> at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection .java:1626)<p><br></p> The 500 errors here generally indicate that there is something wrong with your code. You may have written something wrong. At this time, you need to be careful and careful and then try to use different methods to eliminate the error. You need to check more places on Baidu. The problem here is because the input stream is written in front of the output stream (for the client, the output stream should be written first, and then the input stream get.InputStream())<p><span style="font-size: 16px"></span></p> 2. Bug for axis1.4: AxisFault faultCode: {}Client.NoSOAPAction faultSubcode: faultString: no SOAPAction header! faultActor: faultNode: faultDetail: {}stackTrace:no SOAPAction header!<p><span style="font-size: 16px"></span> 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)</p> <p> //Set SOAPAction Header Otherwise, the error will not be reported without this soapaction header</p> httpConn.setRequestProperty("SOAPAction", "");<p><br> 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</p> <p> //Get the output stream</p> OutputStream out = httpConn.getOutputStream();<p> //Send data Be careful to bring the encoding utf-8 here, otherwise you cannot pass Chinese parameters.<br> out.write(data.getBytes("UTF-8"));<br><br> </p> <p> </p> <p>Question:<span style="font-size: 16px"><strong></strong></span> 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</p> <p>For example:</p> <p></p> <pre class="brush:php;toolbar:false">// 但是为了保险 希望大家在写请求消息体的时候用 接口测试工具去获取比如soapui 然后直接复制过来 String data = "<envelope>" +"<header></header>" +"<body>" +"<say>"//比如这里要调用的方法没有参数 就直接不用写就好 但是这个消息体 应该还是要的 +"</say>" +"</body>" +"</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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software