JAVA调用WebService

   最近项目中需要其他系统的WebService,以前都是直接调用HTTP,然后自己找了如下方法调用,也是对自己学习的一点笔记。工程需要依赖apache的包,下载地址:http://download.csdn.net/detail/zhangyong125/9169431

 代码如下:

 第一个是调用工具类:

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class WebServiceUtil {
	/**
	 * 远程调用WebService接口
	 * 参数可通过如http://localhost:8060/WebService/tes/HRWebServiceForLandray.asmx
	 *
	 * @param parameters           接口需要的参数名数组
	 * @param parametersValue  接口需要的参数所对应的参数值数组
	 * @param operationName    调用接口所对应的方法
	 * @param namespaceURI      WebService默认命名空间
	 * @param endpoint               WebService服务地址
	 * @return jsonStr
	 */
	public static String callWebService(String[] parameters, Object[] parametersValue,
			String operationName, String namespaceURI, String endpoint) {
		final String soapActionURI = namespaceURI + operationName;
		String result = "no result!";
		Service service = new Service();
		Call call;
		try {
			call = (Call) service.createCall();
			call.setTargetEndpointAddress(endpoint);// 远程调用路径
			call.setSOAPActionURI(soapActionURI);
			call.setOperationName(new QName(namespaceURI, operationName));
			for (int i = 0; i < parameters.length; i++) {
				call.addParameter(new QName(namespaceURI, parameters[i]), XMLType.SOAP_STRING, ParameterMode.IN);
			}
			call.setReturnType(XMLType.SOAP_STRING);// 返回值类型:String
			result = (String) call.invoke(parametersValue);// 远程调用,获取json串
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return result;
	}
}
 第二个是测试类:



public class WebService {
	public static String endpoint = "http://localhost8060/test.asmx";
	public static String namespaceURI = "http://tempuri.org/";
	public static String key="809809808089800809";

	public static void main(String[] args) {
		String[] parameters = {"strJson", "strLoginAccount", "strVerifyCode", "strType"};
		Object[] parametersValue = {"1989898989", "17627363727",key, "im"};
		String result = WebServiceUtil.callWebService(parameters, parametersValue, "UpdateMoblie", namespaceURI, endpoint);
		System.out.println(result);
	}
}



Java调用Web服务的常用方法有两种。方法一是使用Java自带的JAX-WS库来调用WebService,示例代码如下: ```java // 调用webservice服务方法1 @Test public void method1() { // 创建webservice代理工厂 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); // 设置远程访问服务地址 factory.setAddress("http://localhost:8080/ws/?wsdl"); // 设置接口类型 factory.setServiceClass(TestService.class); // 生成代理对象 TestService service = factory.create(TestService.class); // 远程访问服务端方法 String word = service.getWord(); System.out.println(word); } ``` 方法二是直接模拟HTTP请求调用WebService。这种方式在请求接口数量较少的情况下比较常用,具体代码如下: ```java // 调用webservice服务方法2: 模拟HTTP请求 @Test public void method2() throws IOException { // 定义请求URL URL url = new URL("http://localhost:8080/ws/?wsdl"); HttpURLConnection connection = null; connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); String soapXML = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:getWord xmlns:ns2=\"http://service.hotriver/\"/></soap:Body></soap:Envelope>"; connection.getOutputStream().write(soapXML.getBytes()); if (connection.getResponseCode() == 200) { Scanner scanner = new Scanner(connection.getInputStream()); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } } ``` 以上是两种常用的Java调用WebService的方法。方法一使用JAX-WS库,方法二是直接模拟HTTP请求。具体选择哪种方法取决于具体的需求和情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java调用webservice.zip](https://download.csdn.net/download/chengxuyuanlaow/87029161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java实现webservice调用](https://blog.csdn.net/qq_44535925/article/details/124784730)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值