C# 发送xml报文到用友U8生成凭证系列一(配置信息)
C# 发送xml报文到用友U8生成凭证系列二(基础代码)
C# 发送xml报文到用友U8生成凭证系列三(Modal代码)
C# 发送xml报文到用友U8生成凭证系列四(Biz代码)最终代码
上文我们详细讲解了u8voucher.xml、Web.config、U8EAI.asmx、EAIHandler.cs 等代码及xml报文格式及相关config配置等,接下来我们将讲解Config、Route、Common 目录下的类怎么编写的:
config目录下的文件:
appSetting.config代码:
这里是配置的用友U8的eai报文导入生成凭证的信息
http://192.168.110.21/u8eai/import.asp 根据你的用友U8安装的服务器IP来设置
<appSettings>
<add key="url" value="http://192.168.110.21/u8eai/import.asp"/>
<add key="encoding" value="utf-8"/>
<add key="voucher" value="NC2U8.Biz.Convert.VoucherConvertor"/>
</appSettings>
U8Voucher.xsd 链接数据库 表
Route 目录下的代码:
Router.cs 代码:
using NC2U8.Biz.Convert;
using System;
using System.Collections.Generic;
using System.Web;
namespace NC2U8.Route
{
public class Router
{
public IConvertor GetConvertor(string type)
{
string className = System.Configuration.ConfigurationManager.AppSettings[type];
if (string.IsNullOrEmpty(className))
throw new NotSupportedException("不存在的报文类型。传入类型:"+type);
IConvertor convertor = Activator.CreateInstance(Type.GetType(className)) as IConvertor;
return convertor;
}
}
}
Common 目录下的代码:
HttpHelper.cs 代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace NC2U8.Common
{
public class HttpHelper
{
public static HttpWebResponse Post(string url,string data)
{
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] btData = Encoding.UTF8.GetBytes(data);
request.ContentLength = btData.Length;
using(var stream = request.GetRequestStream())
{
stream.Write(btData, 0, btData.Length);
}
return request.GetResponse() as HttpWebResponse;
}
public static string GetResponseContent(HttpWebResponse response, string encodingName)
{
byte[] buffer = new byte[4096];
using (Stream stream = response.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, count);
}
Encoding ec = Encoding.GetEncoding(encodingName);
string content = ec.GetString(ms.ToArray());
return content;
}
}
}
}
}
Ok 这些代码写完,接下来下一篇文章将要讲解最重要的环节代码:怎么转换XML节点为U8凭证。