阿里区块链开放联盟使用http方式对接

发布时间 2023-07-06 17:53:34作者: 晚霞起风了

using (HttpClient httpClient = new HttpClient())
{
/*所有的步骤实例都得进行http请求,当前demo中我的请求放在最后,只是一步一步的流程进行开发*/

SendEntity entity = new SendEntity();

#region 步骤一:调用合约接口,将数据上链操作 成功实现合约调用的列子(目前注释起来了)
//插入数据
//entity.methodSignature = "add(string,string,uint256)";
//entity.inputParamListStr = "[\"X0001\",\"张华\",20]";
//entity.accessId = AccessId;
//entity.contractName = "测试正常简单合约";
//entity.token = token;//来源于区块链签名(可以参考文章:https://www.cnblogs.com/lanyue-ik/p/17513333.html)
//entity.orderId = Guid.NewGuid().ToString("N");
//entity.mykmsKeyId = "M04b7z1687834090709";
//entity.outTypes = "[\"string\"]";
//entity.tenantid = "DQGXEH";
//entity.account = "18161217";
//entity.gas = "21000";
//entity.method = "CALLCONTRACTBIZASYNC";
#endregion

#region 步骤二:根据合约接口查询插入的数据
//查询数据-获取查询数据的data
entity.methodSignature = "select(string)";
entity.inputParamListStr = "[\"X0001\"]";
entity.accessId = AccessId;
entity.contractName = "测试正常简单合约";
entity.token = token;
entity.orderId = Guid.NewGuid().ToString("N");
entity.mykmsKeyId = "M04b7z5GRIXEH1687834090709";
entity.outTypes = "[\"string\",\"uint256\"]";
entity.tenantid = "DQGXEH";
entity.account = "181611217";
entity.gas = "210000";
entity.method = "CALLCONTRACTBIZASYNC";
#endregion

#region 步骤三:将步骤二中查询出来的hash值取出来放到当前请求中;交易查询
//将查询数据请求返回的data传递过来进行交易查询
//QueryEntity query = new QueryEntity();
//query.accessId = AccessId;
//query.hash = "c5196326b784d58901437f8ca91e19c4687124016a40557c634de410df32f";
//query.token = token;
//query.method = "QUERYRECEIPT";
#endregion

#region 步骤四:将不走三中查询出来的out参数传入当前请求进行解密;解析合约返回值
//将交易查询接口获取过来的output参数传入解析接口获取明文
//DesEntity desEntity = new DesEntity();
//desEntity.accessId = AccessId;
//string base64 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG5byg5Y2OAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
//byte[] bytes = Convert.FromBase64String(base64);
//string temp = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
//desEntity.content = temp;
//desEntity.abi = "[\"string\",\"uint256\"]";
//desEntity.token = token;
#endregion

//发送的内容
HttpContent content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

string retstr = httpClient.PostAsync("https://rest.baas.alipay.com/api/contract/chainCallForBiz", content).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(retstr);
}