开始8.1开发快一个月了,乘着有空做个资料整理吧。。。
- Http通信部分
http通信部分主要是与.Net平台下开发的服务端通信,采用的是webservice技术,主要分为三部分:1.报文拼写;2.http Post; 3.解析报文
Part_1
public static StringBuilder getLoginPermission(String userid, String password)
{
StringBuilder str = new StringBuilder();
str.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
str.Append("<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\">");
str.Append(" <soap12:Body>");
str.Append(" <GetLoginPermission xmlns=\"XXX.XXXXX\">");//保密
str.Append(" <user>");
str.Append(" <userID></userID>");
str.Append(" <cryptUserID>" + userid + "</cryptUserID>");
str.Append(" <tagID></tagID>");
str.Append(" <password>" + password + "</password>");
str.Append(" </user>");
str.Append(" </GetLoginPermission>");
str.Append(" </soap12:Body>");
str.Append("</soap12:Envelope>");
//LogU.Info("getLoginPermission XML: " + str);
return str;
}
Part_2
public async static Task<string> postSoapData12(string setAction, StringBuilder sXml, string servername)
{
LogU.Info("postSoapData12 Enter");
StringBuilder ret = new StringBuilder();
try
{
HttpClient mClient = new HttpClient();
//server uri
Uri uri = new Uri(servername);
//post contentS
HttpStringContent strContent = new HttpStringContent(sXml.ToString(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "text/xml");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Content = strContent;
HttpResponseMessage resp = await mClient.SendRequestAsync(request).AsTask(mCancel.Token);
string responseBody = await resp.Content.ReadAsStringAsync().AsTask(mCancel.Token);
LogU.Info("responseBody: " + responseBody);
return responseBody;
}
WebException
catch (Exception ex)
{
throw ex;
}
}
Part_3
public static string ReadXMLforString(string sXmlFile, string sTag)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sXmlFile);
string result = xmlDoc.GetElementsByTagName(sTag)[0].InnerText;
return result;
}
// 例外
catch (Exception ex)
{
return string.Empty;
}
}
其他Xml解析
public static List<Dictionary<string, string>> ReadXMLforListDic(string sXmlFile, string sTag)
{
try
{
List<Dictionary<string, string>> reList = new List<Dictionary<string, string>>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sXmlFile);
XmlNodeList listObs = xmlDoc.GetElementsByTagName(sTag);
if (listObs != null && listObs.Count > 0)
{
uint index = 0;
int count = 0;
for (uint i = 0; i < listObs.Count; i++)
{
if (count < listObs.Item(i).ChildNodes.Count)
{
index = i;
count = listObs.Item(i).ChildNodes.Count;
}
}
IXmlNode xmlData = listObs.Item(index);
for (int i = 0; i < xmlData.ChildNodes.Count; i++)
{
IXmlNode dataNode = xmlData.ChildNodes[i];
}
foreach (IXmlNode nodeObs in listObs)
{
Dictionary<string, string> itemDic = new Dictionary<string, string>();
for (int j = 0; j < nodeObs.ChildNodes.Count; j++)
{
IXmlNode dataNode = nodeObs.ChildNodes[j];
itemDic.Add(dataNode.NodeName, dataNode.InnerText);
}
reList.Add(itemDic);
}
}
return reList;
}
catch (Exception ex)
{
return null;
}
}
- Base64加密解密
private static byte[] key_128 = { (byte)0xcc, (byte)0xfc, (byte)0xbd, (byte)0xcf, (byte)0x19, (byte)0x8b, (byte)0xfa, (byte)0x0b, (byte)0xab, (byte)0x24, (byte)0x7d, (byte)0xf6, (byte)0x28, (byte)0xd1, (byte)0x57, (byte)0xfb, };
private static byte[] IV = { (byte)0x99, (byte)0x23, (byte)0x99, (byte)0x1f, (byte)0x7b, (byte)0xa8, (byte)0x34, (byte)0xb2, (byte)0x7d, (byte)0x19, (byte)0x31, (byte)0x05, (byte)0x81, (byte)0xdb, (byte)0x02, (byte)0x9c };
public static String Encrypt(string text)
{
String strAlgName = SymmetricAlgorithmNames.AesCbcPkcs7;
BinaryStringEncoding encoding;
CryptographicKey key;
IBuffer iv;
IBuffer mText = SampleCipherEncryption(
text,
strAlgName,
128,
out encoding,
out iv,
out key);
return CryptographicBuffer.EncodeToBase64String(mText);
}
public static IBuffer SampleCipherEncryption(
String strMsg,
String strAlgName,
UInt32 keyLength,
out BinaryStringEncoding encoding,
out IBuffer iv,
out CryptographicKey key)
{
// Initialize the initialization vector.
iv = null;
// Initialize the binary encoding value.
encoding = BinaryStringEncoding.Utf8;
// Create a buffer that contains the encoded message to be encrypted.
IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);
// Open a symmetric algorithm provider for the specified algorithm.
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);
// Demonstrate how to retrieve the name of the algorithm used.
String strAlgNameUsed = objAlg.AlgorithmName;
// Determine whether the message length is a multiple of the block length.
// This is not necessary for PKCS #7 algorithms which automatically pad the
// message to an appropriate length.
if (!strAlgName.Contains("PKCS7"))
{
if ((buffMsg.Length % objAlg.BlockLength) != 0)
{
throw new Exception("Message buffer length must be multiple of block length.");
}
}
// Create a symmetric key.
//IBuffer keyMaterial = CryptographicBuffer.GenerateRandom(keyLength);
IBuffer keyMaterial = CryptographicBuffer.CreateFromByteArray(key_128);
key = objAlg.CreateSymmetricKey(keyMaterial);
// CBC algorithms require an initialization vector. Here, a random
// number is used for the vector.
if (strAlgName.Contains("CBC"))
{
iv = CryptographicBuffer.CreateFromByteArray(IV);
}
// Encrypt the data and return.
IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, buffMsg, iv);
return buffEncrypt;
}
public static String SampleCipherDecryption(
String strAlgName,
IBuffer buffEncrypt,
IBuffer iv,
BinaryStringEncoding encoding,
CryptographicKey key)
{
// Declare a buffer to contain the decrypted data.
IBuffer buffDecrypted;
// Open an symmetric algorithm provider for the specified algorithm.
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);
// The input key must be securely shared between the sender of the encrypted message
// and the recipient. The initialization vector must also be shared but does not
// need to be shared in a secure manner. If the sender encodes a message string
// to a buffer, the binary encoding method must also be shared with the recipient.
buffDecrypted = CryptographicEngine.Decrypt(key, buffEncrypt, iv);
// Convert the decrypted buffer to a string (for display). If the sender created the
// original message buffer from a string, the sender must tell the recipient what
// BinaryStringEncoding value was used. Here, BinaryStringEncoding.Utf8 is used to
// convert the message to a buffer before encryption and to convert the decrypted
// buffer back to the original plaintext.
String strDecrypted = CryptographicBuffer.ConvertBinaryToString(encoding, buffDecrypted);
return strDecrypted;
}
/// <summary>
/// 文字列をAESで復号化
/// </summary>
/// <param name="text">復号化対象の文字列</param>
public static String Decrypt(string text)
{
//string strMsg = text; // Data to encrypt.
string strAlgName = SymmetricAlgorithmNames.AesCbcPkcs7; //decrypt algorith method
UInt32 keyLength = 128; // Length of the key, in bytes
BinaryStringEncoding encoding; // Binary encoding value
IBuffer iv; // Initialization vector
CryptographicKey key;
IBuffer bfText = CryptographicBuffer.DecodeFromBase64String(text);
SampleCipherEncryption(text, strAlgName, keyLength, out encoding, out iv, out key);
return SampleCipherDecryption(
strAlgName,
bfText,
iv,
encoding,
key
);
}