Axis2 java调用.net webservice接口的问题2(Face To)

 

<span style="font-family: Arial, Helvetica, sans-serif;">import java.text.SimpleDateFormat;</span>
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
public class BaseService {
	public static EndpointReference targetEPR = new EndpointReference("http://192.168.0.185/OnlinePaywebservice/platformws.asmx");
	//public static EndpointReference targetEPR = new EndpointReference("http://192.168.60.69/OnlinePaywebservice/platformws.asmx");
	public static String tns = "http://tempuri.org/";
	private static String key="e99edc20b0134ce1865a7071bb950fd0";

	public static void addValidation(ServiceClient serviceClient) {
		String appIdString = "145";
		String appNameString = "门户平台";
		String appTypeIdString = "2";
		Date nowDate = Calendar.getInstance().getTime();
		String timestamp0 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(nowDate);
		String timestamp1 = new SimpleDateFormat("yyyy-MM-dd").format(nowDate);
		String timestamp2 = new SimpleDateFormat("hh:mm:ss").format(nowDate);
		String timestampString = timestamp1 + "T" + timestamp2;
		Random r = new Random();
		String randomString = String.valueOf(r.nextInt(10000));
		String verString = "1.0.0.0";
		//
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(tns, "");
		OMElement header = fac.createOMElement("CredentialSoapHeader", omNs);
		OMElement appId = fac.createOMElement("AppID", omNs);
		OMElement appName = fac.createOMElement("AppName", omNs);
		OMElement appTypeId = fac.createOMElement("AppTypeID", omNs);
		OMElement timestamp = fac.createOMElement("Timestamp", omNs);
		OMElement random = fac.createOMElement("Random", omNs);
		OMElement sign = fac.createOMElement("Sign", omNs);
		OMElement ver = fac.createOMElement("Ver", omNs);
		//
		appId.setText(appIdString);
		appName.setText(appNameString);
		appTypeId.setText(appTypeIdString);
		timestamp.setText(timestampString);
		random.setText(randomString);
		ver.setText(verString);

		String signString = "AppID=" + appIdString + "&AppName=" + appNameString + "&AppTypeID=" + appTypeIdString + "&Timestamp=" + timestamp0 + "&Random=" + randomString + "&Ver=" + verString;
		signString = signString + "&EncodedMustUnderstand=0&EncodedMustUnderstand12=0&MustUnderstand=False&Actor=&Role=&DidUnderstand=True&EncodedRelay=0&Relay=False";
		System.out.println("签名原串:" + signString);
		String signResult = GetSign(signString);
		System.out.println("签名后:" + signResult);
		sign.setText(signResult);

		header.addChild(appId);
		header.addChild(appName);
		header.addChild(appTypeId);
		header.addChild(timestamp);
		header.addChild(random);
		header.addChild(sign);
		header.addChild(ver);
		System.out.println("header:" + header.toString());
		serviceClient.addHeader(header);
	}

	/**
	 * 接入标识:145,系统名称:门户平台,接入授权码: 6181a1fb89564b589283ad578baa7d5e,应用类型:101
	 * 
	 * @param signStr
	 */
	public static String GetSign(String signStr) {
		String MethodName="GetSign";
		Options options = new Options();
		options.setAction(tns+MethodName);
		options.setTo(targetEPR);
		options.setProperty(HTTPConstants.CHUNKED, "false");

		ServiceClient sender = null;
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace(tns, "");
			OMElement method = fac.createOMElement(MethodName, omNs);
			OMElement name = fac.createOMElement("prestr", omNs);
			OMElement name2 = fac.createOMElement("key", omNs);
			name.setText(signStr);// 设置入参值
			name2.setText(key);

			method.addChild(name);
			method.addChild(name2);
			method.build();
			OMElement response = sender.sendReceive(method);
			System.out.println("签名响应:" + response);
			OMElement elementReturn = response.getFirstElement();
			return elementReturn.getText();
		} catch (AxisFault e) {
			System.out.println("Error");
			e.printStackTrace();
			return null;
		}
	}
}

 

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

public class OnlinePayService extends BaseService {

	/**
	 * 获取OLID
	 * 
	 * @param olNo
	 */
	public static String GetAccountByOLNO(String olNo) {
		String MethodName="GetAccountByOLNO";
		Options options = new Options();
		options.setAction(tns+MethodName);
		options.setTo(targetEPR);
		options.setProperty(HTTPConstants.CHUNKED, "false");

		ServiceClient sender = null;
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			//SOAP头
			addValidation(sender);
			//SOAP体参数
			OMNamespace omNs = fac.createOMNamespace(tns, "");
			OMElement method = fac.createOMElement(MethodName, omNs);
			OMElement name = fac.createOMElement("OLNO", omNs);
			name.setText(olNo);

			method.addChild(name);
			method.build();
			OMElement response = sender.sendReceive(method);
			System.out.println("获取OLID响应:" + response);
			OMElement elementReturn = response.getFirstElement();
			System.out.println("获取签名:" + elementReturn.getText());
			return elementReturn.getText();
		} catch (Exception e) {
			System.out.println("Error");
			e.printStackTrace();
			return null;
		}
	}


	public static void main(String arg[]) {
		OnlinePayService.GetAccountByOLNO("303032");
	}
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值