通过URL的方式请求WebService服务

1.创建服务端

package com.test;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class WebTest {

@WebMethod
public void testWebservice(String data){
System.out.print("接受到请求:" + data);
}

public static void main(String[] args) {
System.out.println("开始发布");
WebTest test = new WebTest();
Endpoint.publish("http://localhost:8000/test", test);
System.out.println("发布成功");
}
}
怎么测试是否发布成功? 在浏览器中键入: http://localhost:8000/ test?wsdl
如果发布成功可以看见下面结果:
 <?xml version="1.0" encoding="UTF-8" ?>
<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!--Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test.com/" name="WebTestService">
<types>
<xsd:schema>
<xsd:import namespace="http://test.com/" schemaLocation="http://localhost:8000/test?xsd=1" />
</xsd:schema>
</types>
<message name="testWebservice">
<part name="parameters" element="tns:testWebservice" />
</message>
<message name="testWebserviceResponse">
<part name="parameters" element="tns:testWebserviceResponse" />
</message>
<portType name="WebTest">
<operation name="testWebservice">
<input message="tns:testWebservice" />
<output message="tns:testWebserviceResponse" />
</operation>
</portType>
<binding name="WebTestPortBinding" type="tns:WebTest">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="testWebservice">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="WebTestService">
<port name="WebTestPort" binding="tns:WebTestPortBinding">
<soap:address location="http://localhost:8000/test" />
</port>
</service>
</definitions>

2.客户端通过URLConnection调用

package com.ceb.czsczj.client.init;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ceb.czsczj.client.bo.AccountInfoBO;
import com.ceb.czsczj.client.common.util.DateTools;
/**一个简单的请求的例子还没有做完*/
public class ClientTest {

/**
 * @Description: demo
 * @throws IOException 
 * @throws DocumentException 
 * @author fengjiahuan
 * @date 2018-2-9上午10:49:25
 *===========================================
 * 修改人:,    修改时间:,    修改版本:
 * 修改备注:
 *===========================================
 */
public static void main(String[] args) throws IOException, DocumentException {
ArrayList<AccountInfoBO> list = new ArrayList<AccountInfoBO>();
Map<Object, Object> resMap = new HashMap<Object, Object>();
resMap.put("result", "error");
//创建url地址
URL url = new URL("http://localhost:8000/test");
//打开连接
URLConnection conn = url.openConnection();
//转换成HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
//打开输入输出的开关
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//设置请求方式
httpConn.setRequestMethod("POST");
//设置请求的头信息
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
String date = DateTools.getSysDateStr();
date = date.replaceAll("-", "");
String data = "<burlap:call>" +
    "<method>createBusinessData</method>"  + 
    "<string>BANK.****</string>" +
    "<string>ABANKTEST</string>" + 
    "<int>"+date.substring(0, 4)+"</int>" +  
    "<list>"+  "</list>" + "</burlap:call>";
//获得输出流
OutputStream out = httpConn.getOutputStream();
//发送数据
out.write(data.getBytes());
out.close();
System.out.println(data);
//判断请求成功
if(httpConn.getResponseCode() == 200){
System.out.println("sss");
    //获得输入流
    InputStream in = httpConn.getInputStream();
    //使用输入流的缓冲区
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    in.close();
    StringBuffer sb = new StringBuffer();
    String line = null;
    //读取输入流
    while((line = reader.readLine()) != null){
        sb.append(line);
    }
    //创建sax的读取器
    SAXReader saxReader = new SAXReader();
    //创建文档对象
    Document doc = saxReader.read(new StringReader(sb.toString()));
    //获得请求响应return元素
    List<Element> eles = doc.selectNodes("//list");
    for(Element ele : eles){
        System.out.println(ele.getText());
    }
}
httpConn.disconnect();
/*返回报文数据项*/
resMap.put("result", "success");
System.out.println("请求成功!!");
}
}

下面是一个通过拼接XML请求Web服务的Java代码示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class WebServiceClient { public static void main(String[] args) { try { URL url = new URL("http://example.com/MyWebService"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml"); String requestXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://example.com/webservice\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <web:MyOperation>\n" + " <web:Param1>Value1</web:Param1>\n" + " <web:Param2>Value2</web:Param2>\n" + " </web:MyOperation>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; connection.setDoOutput(true); connection.getOutputStream().write(requestXML.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个例子中,我们首先创建一个URL对象并打开一个HttpURLConnection。设置HTTP请求方法为POST并设置Content-Type为text/xml。 然后,我们创建一个包含所需参数的XML字符串,并将其发送到Web服务。 最后,我们读取响应并在控制台上打印它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值