IdCardCheckForThree.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Security;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using Library;
  10. // using MongoDB.Bson;
  11. // using MongoDB.Driver;
  12. // using MongoDB.Driver.Linq;
  13. namespace MySystem
  14. {
  15. public class IdCardCheckForThree
  16. {
  17. public readonly static IdCardCheckForThree Instance = new IdCardCheckForThree();
  18. private IdCardCheckForThree()
  19. {
  20. }
  21. private const String host = "https://dfphone3.market.alicloudapi.com";
  22. private const String path = "/verify_id_name_phone";
  23. private const String method = "POST";
  24. private const String appcode = "8e5704921ca3422f80f0deb935a7ddc6";
  25. public string Do(string mobile, string idcard, string name)
  26. {
  27. String querys = "";
  28. function.WriteLog(DateTime.Now.ToString(), "IdCard验证三要素日志");
  29. function.WriteLog(querys, "IdCard验证三要素日志");
  30. String bodys = "id_number=" + idcard + "&name=" + name + "&phone_number=" + mobile + "";
  31. String url = host + path;
  32. HttpWebRequest httpRequest = null;
  33. HttpWebResponse httpResponse = null;
  34. if (0 < querys.Length)
  35. {
  36. url = url + "?" + querys;
  37. }
  38. if (host.Contains("https://"))
  39. {
  40. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  41. httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  42. }
  43. else
  44. {
  45. httpRequest = (HttpWebRequest)WebRequest.Create(url);
  46. }
  47. httpRequest.Method = method;
  48. httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
  49. //根据API的要求,定义相对应的Content-Type
  50. httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  51. if (0 < bodys.Length)
  52. {
  53. byte[] data = Encoding.UTF8.GetBytes(bodys);
  54. using (Stream stream = httpRequest.GetRequestStream())
  55. {
  56. stream.Write(data, 0, data.Length);
  57. }
  58. }
  59. try
  60. {
  61. httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  62. }
  63. catch (WebException ex)
  64. {
  65. httpResponse = (HttpWebResponse)ex.Response;
  66. }
  67. Stream st = httpResponse.GetResponseStream();
  68. StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
  69. string result = reader.ReadToEnd();
  70. function.WriteLog(result, "IdCard验证三要素日志");
  71. function.WriteLog("\n\n", "IdCard验证三要素日志");
  72. return result;
  73. }
  74. public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  75. {
  76. return true;
  77. }
  78. }
  79. }