Repository navigation
soap
Aichaellee edited this page Jan 21, 2019
·
2 revisions
package net.cnki.common.validator;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.cnki.bean.ClientMsg;
import net.cnki.common.util.RSAUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;
import javax.xml.soap.*;
import java.io.*;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class TestSmsCodeSender {
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static StringBuilder value=new StringBuilder();
public static void main(String[] args) throws SOAPException ,Exception{
String rsa = getRsa();
System.out.println("osdijaskdlfjasdf==========="+rsa);
ObjectMapper om = new ObjectMapper();
String afterSer = om.writeValueAsString(new ClientMsg());
RSAUtil rsaUtil=new RSAUtil();
byte[] data=rsaUtil.encrypt(afterSer.getBytes(),rsa);
// smsCode
String originXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <SendSms xmlns=\"http://tempuri.org/\">\n" +
" <rsaClientMsg>"+data+"</rsaClientMsg>\n" +
" </SendSms>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
//byte[] data1=rsaUtil.decrypt(data);
// String str=new String(data,"UTF-8");
// System.out.println("--------------"+str);
String rsaXml = originXml.replace("needSendMsg",new String(data));
System.out.println("====the after xml is:"+rsaXml);
String Smsurl = "http://192.168.25.115:8096/SMSService.asmx?op=SendSms";
String SmsSOAPAction = "http://tempuri.org/SendSms";
doPostSoap1_2(Smsurl,rsaXml,SmsSOAPAction);
}
// getrsa
public static String getRsa() throws SOAPException{
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <GetRsaPublicKey xmlns=\"http://tempuri.org/\" />\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
String url = "http://192.168.25.115:8096/SMSService.asmx?op=GetRsaPublicKey";
String SOAPAction = "http://tempuri.org/GetRsaPublicKey";
String retStr = doPostSoap1_2(url,xml,SOAPAction);
SOAPMessage msg = formatSoapString(retStr);
SOAPBody body = msg.getSOAPBody();
Iterator<SOAPElement> iterator = body.getChildElements();
String rsa = PrintBody(iterator, null);
System.out.println("the rsa is:"+rsa);
String result = StringUtils.substringBetween(rsa,"<Modulus>","</Modulus>");
System.out.println("after between: "+result);
//加密后的字符串
String afterLow = result.toLowerCase() + "@check.cnki";
String encodeStr=DigestUtils.md5Hex(afterLow.substring(7,15));
return rsa;
}
// 发送soap
public static String doPostSoap1_2(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type",
"application/soap+xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println("response:" + retStr);
}
// Md5("12345678.ToLower() + "@check.cnki", "md5").Substring(7, 15)
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
System.out.println("exception in doPostSoap1_2"+ e);
}
return retStr;
}
private static SOAPMessage formatSoapString(String soapString) {
MessageFactory msgFactory;
try {
msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
new ByteArrayInputStream(soapString.getBytes("UTF-8")));
reqMsg.saveChanges();
return reqMsg;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String PrintBody(Iterator<SOAPElement> iterator, String side) {
while (iterator.hasNext()) {
SOAPElement element = (SOAPElement) iterator.next();
System.out.println("Local Name:" + element.getLocalName());
System.out.println("Node Name:" + element.getNodeName());
System.out.println("Tag Name:" + element.getTagName());
System.out.println("Value:" + element.getValue());
if(StringUtils.equals(element.getNodeName(),"GetRsaPublicKeyResult")){
value.append(element.getValue());
}
if (null == element.getValue()
&& element.getChildElements().hasNext()) {
PrintBody(element.getChildElements(), side + "-----");
}
}
return value.toString();
}
}