DEMO: Factor/Idcard - 身份证二要素
概览
- 支持.Net版本:2.0以上
- 依赖三方库 Newtonsoft.Json.dll
代码示例
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
namespace SUBMAILDEMO
{
public class FactorIdCardDemo
{
private const string API_FactorMobile = "https://tpa.mysubmail.com/factor/idcard.json";
private const string API_Timestamp = "https://api.mysubmail.com/service/timestamp.json";
public void Request()
{
string appid = "";
string appkey = "";
string name = "";
string idNo = "";
string timestamp = JObject.Parse(HttpGet(API_Timestamp))["timestamp"].ToString();
string signStr = "appkey=" + appkey + "&idNo=" + idNo + "&name=" + HttpUtility.UrlEncode(name, Encoding.UTF8).ToUpper() + "×tamp=" + timest
string signature = GetSHA256Signature(signStr);
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("appid", appid);
d.Add("name", name);
d.Add("idNo", idNo);
d.Add("signature", signature);
d.Add("timestamp", timestamp);
string response = Httppost(API_FactorMobile, d);
Console.WriteLine(response);
}
//http post请求
public string Httppost(string url, Dictionary<string, string> parameters)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求对象
request.Method = "POST";//请求方式
request.ContentType = "application/x-www-form-urlencoded";//链接类型
//构造查询字符串
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
bool first = true;
foreach (string key in parameters.Keys)
{
if (!first)
{
buffer.AppendFormat("&{0}={1}", HttpUtility.UrlEncode(key, Encoding.UTF8), HttpUtility.UrlEncode((string)parameters[key], Encoding.UTF8));
}
else
{
buffer.AppendFormat("{0}={1}", HttpUtility.UrlEncode(key, Encoding.UTF8), HttpUtility.UrlEncode((string)parameters[key], Encoding.UTF8));
first = false;
}
}
Console.WriteLine("requestParam:" + buffer.ToString());
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
//写入请求流
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
}
HttpWebResponse hwp = request.GetResponse() as HttpWebResponse;
string response = GetResponseString(hwp);
hwp.Close();
return response;
}
//获取服务器时间戳
public string HttpGet(string url)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求对象
request.Method = "GET";//请求方式
request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse hwp = request.GetResponse() as HttpWebResponse;
string response = GetResponseString(hwp);
hwp.Close();
return response;
}
public string GetResponseString(HttpWebResponse webresponse)
{
using (Stream s = webresponse.GetResponseStream())
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
}
//获取sha1加密后的签名字符串
private string GetSHA256Signature(string signStr)
{
SHA256 sha256 = SHA256.Create();
byte[] fromData = Encoding.GetEncoding("utf-8").GetBytes(signStr);
byte[] targetData = sha256.ComputeHash(fromData);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < targetData.Length; i++)
{
sBuilder.Append(targetData[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
}