版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34379916/article/details/78641571
最近需要和一个系统对接,对方给的接口文档是wcf的服务端,需要传递自定义的对象,并且需要md5加密后进行传输,在网上找了很久,在此记录一下,希望以后需要的人能少走些弯路
主要进行网络请求的类:
package com.example.com.classs;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class Service3 implements ISoapServer {
// private static final String NameSpace = "http://tempuri.org/";
// private static final String URL =
// "http://fszhangshouning.fbse.com/testWebService/Service1.svc";
// private static final String SOAP_ACTION =
// "http://tempuri.org/Service1/GetUser";
// private static final String MethodName = "GetUser";
// private static final String MAPURL =
// "http://schemas.datacontract.org/2004/07/WcfService1";
private static final String NameSpace = "http://tempuri.org/";
// private static final String URL =
// "http://pms.beyondh.com:7998/Service/Order/HotelService.svc/Basic";
// private static final String SOAP_ACTION =
// "http://tempuri.org/IHotelService/GetCheckinGuest";
// private static final String MethodName = "GetAllHotels";
private static final String URL = "http://pms.beyondh.com:7998/Service/Order/OrderService.svc/Basic";
private static final String SOAP_ACTION = "http://tempuri.org/IOrderService/GetCheckinGuest";
private static final String MethodName = "GetCheckinGuest";
private static final String MAPURL = "http://schemas.datacontract.org/2004/07/NextPms.PublicService.Model.Order";
// 285716887011329
@Override
public SoapObject LoadResult() {
SoapObject request = new SoapObject(NameSpace, MethodName);
CheckinGuestRequest check = new CheckinGuestRequest();
check.setBeginTime("2017-11-05T01:01:01");
check.setCheckinStatus(new String[]{"I"});
check.setHotelId(2147483644l);
PropertyInfo pi = new PropertyInfo();
pi.setName("checkinGuestRequest");
pi.setValue(check);
pi.setType(check.getClass());
request.addProperty(pi);
request.addProperty("unixExpireTimestamp", 1511308800l);
System.out.println(getMD5Str(("2017-11-05 01-01-01i21474836441511308800" + "6AF0062B-9C6D-4796-8760-803779CF7E48")));
request.addProperty("signature", "Web:" + getMD5Str(("2017-11-05 01-01-01i21474836441511308800" + "6AF0062B-9C6D-4796-8760-803779CF7E48")));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = request;
envelope.implicitTypes = true;
envelope.addMapping(MAPURL, "checkinGuestRequest", CheckinGuestRequest.CheckinGuestRequest);
MarshalUser_ userMarshal = new MarshalUser_();
userMarshal.register(envelope);
// Android传输对象
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
SoapObject response = null;
try {
transport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
response = (SoapObject) envelope.bodyIn;
return response;
}
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
System.exit(-1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
}
将对象进行xml封装的类:
package com.example.com.classs;
import java.io.IOException;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
public class MarshalUser_ implements Marshal {
private static String MAPURL = "http://schemas.datacontract.org/2004/07/NextPms.PublicService.Model.Order";// 要注意这儿的定义,具体查看wcf端的xml是如何定义的
@Override
public Object readInstance(XmlPullParser parser, String arg1, String arg2, PropertyInfo arg3) throws IOException, XmlPullParserException {
System.out.println("readInstance");
return CheckinGuestRequest.fromString(parser.nextText());
}
@Override
public void register(SoapSerializationEnvelope cm) {
System.out.println("register");
cm.addMapping(MAPURL, "checkinGuestRequest", CheckinGuestRequest.class, this);
}
@Override
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
CheckinGuestRequest user = (CheckinGuestRequest) obj;
writer.startTag(MAPURL, "BeginTime");
writer.text(user.getBeginTime());
writer.endTag(MAPURL, "BeginTime");
writer.startTag(MAPURL, "CheckinStatus");
writer.text(user.getCheckinStatus()[0]);
writer.endTag(MAPURL, "CheckinStatus");
writer.startTag(MAPURL, "HotelId");
writer.text(user.getHotelId() + "");
writer.endTag(MAPURL, "HotelId");
}
}
自定义对象类:
package com.example.com.classs;
import java.util.Date;
/**
* @auther creat by zj
* @date 2017-11-16---下午1:06:39
* @problem
* @answer
**/
public class CheckinGuestRequest {
public static Class CheckinGuestRequest = CheckinGuestRequest.class;
String BeginTime;
String CheckinStatus[];
long HotelId;
public String getBeginTime() {
return BeginTime;
}
public void setBeginTime(String beginTime) {
BeginTime = beginTime;
}
public String[] getCheckinStatus() {
return CheckinStatus;
}
public void setCheckinStatus(String checkinStatus[]) {
CheckinStatus = checkinStatus;
}
public long getHotelId() {
return HotelId;
}
public void setHotelId(long hotelId) {
HotelId = hotelId;
}
public static CheckinGuestRequest fromString(String s) {
CheckinGuestRequest result = new CheckinGuestRequest();
String[] tokens = s.split("=");
String begintime = tokens[1].split(";")[0];
String checkin[] = tokens[2].split(";");
String hotelid = tokens[3].split(";")[0];
result.BeginTime = begintime;
result.CheckinStatus = checkin;
result.HotelId = Integer.parseInt(hotelid);
return result;
}
}