Searcher.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using System.Text.Json;
  8. namespace IPRS.xiaoshi.sz.com
  9. {
  10. public class IPRSSearcher
  11. {
  12. private static string SearchAppId = "K8FFB741E163BE6536";
  13. private static string SearchAppKey = "FNYJD7902206FFB741E163BE6536C3689D55";
  14. string appId = "1000046";
  15. string appKey = "6AE6D4DC6AF94F26862501EDEE9E27A2";
  16. public string? GetPatents(string strCondtion,int page,int size)
  17. {
  18. int tryCount = 0;
  19. begin:
  20. string strSearchUrl = "http://s.patentstar.com.cn/SearchAPI/PatentSearch/ResultGet";
  21. string stamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
  22. string queryJson = "{\"CurrentQuery\":\"" + strCondtion + "\",\"DBType\":\"CN\",\"PageNum\":" +page.ToString() + ",\"RowCount\":" + size.ToString() + ",\"OrderBy\":\"ID\",\"OrderByType\":\"ASC\"}";
  23. string sign = EncryptStringToMD5( SearchAppKey + stamp);
  24. var content = new FormUrlEncodedContent(new[]
  25. {
  26. new KeyValuePair<string, string>("AppID", SearchAppId),
  27. new KeyValuePair<string, string>("Stamp", stamp),
  28. new KeyValuePair<string, string>("Sign", sign),
  29. new KeyValuePair<string, string>("QueryJson", queryJson)
  30. });
  31. using (HttpClient client = new HttpClient() { Timeout=TimeSpan.FromSeconds(5)})
  32. {
  33. try
  34. {
  35. var retTask = client.PostAsync(strSearchUrl, content);
  36. retTask.Wait();
  37. return retTask.Result.Content.ReadAsStringAsync().Result;
  38. }
  39. catch(Exception ex)
  40. {
  41. tryCount ++;
  42. if(tryCount < 5)
  43. {
  44. System.Threading.Thread.Sleep(1000);
  45. goto begin;
  46. }
  47. Console.WriteLine(ex.Message);
  48. }
  49. }
  50. return null;
  51. }
  52. private string GetIPRSData(string url)
  53. {
  54. int iTryCount = 0;
  55. tryAgain:
  56. long currentTimeMillis = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  57. string sign = appId + appKey + currentTimeMillis.ToString();
  58. string signMd5 = EncryptStringToMD5(sign);
  59. try
  60. {
  61. using (HttpClient client = new HttpClient())
  62. {
  63. client.DefaultRequestHeaders.Add("_appid", appId);
  64. client.DefaultRequestHeaders.Add("_timestamp", currentTimeMillis.ToString());
  65. client.DefaultRequestHeaders.Add("_sign", signMd5);
  66. var resTask = client.GetAsync(url);
  67. resTask.Wait();
  68. HttpResponseMessage response = resTask.Result;
  69. response.EnsureSuccessStatusCode();
  70. string responseBody = response.Content.ReadAsStringAsync().Result;
  71. //Console.WriteLine(responseBody);
  72. return responseBody;
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. iTryCount ++;
  78. if(iTryCount < 3)
  79. {
  80. goto tryAgain;
  81. }
  82. return null;
  83. }
  84. }
  85. public string GetPatentCNBiblio(string appNo)
  86. {
  87. string url = $"https://api.patentstar.com.cn/api/Patent/CnBibo/{appNo}";
  88. return GetIPRSData(url);
  89. }
  90. public string GetPatentFullTxtInfo(string appNo)
  91. {
  92. //string url = $"https://api.patentstar.com.cn/api/Patent/CnBibo/{appNo}";
  93. string url = $"https://api.patentstar.com.cn/api/Patent/CnFullXml/{appNo}";
  94. return GetIPRSData (url) ;
  95. }
  96. public string GetPatentCnMainPic(string appNo)
  97. {
  98. string url = $"https://api.patentstar.com.cn/api/Patent/CnMainImage/{appNo}";
  99. return GetIPRSData(url);
  100. }
  101. public string GetPatentCNWGImage(string appNo)
  102. {
  103. string url = $"https://api.patentstar.com.cn/api/Patent/CnWGImage/{appNo}";
  104. return GetIPRSData(url);
  105. }
  106. public string GetPatentCNLegal(string appNo)
  107. {
  108. string url = $"https://api.patentstar.com.cn/api/Patent/CnLegal/{appNo}";
  109. return GetIPRSData(url);
  110. }
  111. public string GetPatentCNPdf(string appNo)
  112. {
  113. string url = $"https://api.patentstar.com.cn/api/Patent/CnPdf/{appNo}";
  114. return GetIPRSData(url);
  115. }
  116. public string GetPatentENPdf(string pubNo)
  117. {
  118. string url = $"https://api.patentstar.com.cn/api/Patent/EnPdf/{pubNo}";
  119. return GetIPRSData(url);
  120. }
  121. public string GetPatentENBiblio(string pubNo)
  122. {
  123. string url = $"https://api.patentstar.com.cn/api/Patent/EnBib/{pubNo}";
  124. return GetIPRSData(url);
  125. }
  126. public string GetPatentFamilys(string pubNo)
  127. {
  128. string url = $"https://api.patentstar.com.cn/api/Patent/FamilyByPubNo/{pubNo}";
  129. return GetIPRSData(url);
  130. }
  131. public string GetPatentCitedNum(string pubNo)
  132. {
  133. string url = $"https://api.patentstar.com.cn/api/Patent/CitedNumByPubNo/{pubNo}";
  134. return GetIPRSData(url);
  135. }
  136. /// <summary>
  137. /// MD5加密
  138. /// </summary>
  139. /// <param name="encryptString">_appid + appkey + _timestamp</param>
  140. /// <returns>_sign</returns>
  141. private string EncryptStringToMD5(string input)
  142. {
  143. // 创建一个MD5实例
  144. using (MD5 md5 = MD5.Create())
  145. {
  146. // 将输入字符串转换为字节数组并计算哈希值
  147. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  148. byte[] hashBytes = md5.ComputeHash(inputBytes);
  149. // 将字节数组转换为十六进制字符串
  150. StringBuilder sb = new StringBuilder();
  151. for (int i = 0; i < hashBytes.Length; i++)
  152. {
  153. sb.Append(hashBytes[i].ToString("X2"));
  154. }
  155. return sb.ToString();
  156. }
  157. }
  158. public List<string> splitCondition(string condition,DateTime startDate,DateTime endDate)
  159. {
  160. string temCond = $"{condition}*{startDate.ToString("yyyyMMdd")}>{endDate.ToString("yyyyMMdd")}/AD";
  161. string? strRet = GetPatents(temCond, 1, 1);
  162. int TotalPatents = 0;
  163. using (JsonDocument document = JsonDocument.Parse(strRet))
  164. {
  165. var retCode = document.RootElement.GetProperty("Ret").GetInt32();
  166. TotalPatents = document.RootElement.GetProperty("Data").GetProperty("HitCount").GetInt32();
  167. }
  168. if (TotalPatents > 10000)
  169. {
  170. DateTime midDate = endDate.Subtract((endDate - startDate)/2);
  171. List<string> list = new List<string>();
  172. List<string> lst1 = splitCondition(condition,startDate,midDate);
  173. List<string> lst2 = splitCondition(condition,midDate,endDate );
  174. list.AddRange(lst1);
  175. list.AddRange(lst2);
  176. return list;
  177. }
  178. else
  179. {
  180. if (TotalPatents > 0)
  181. {
  182. return new List<string>() { temCond };
  183. }
  184. else
  185. {
  186. return new List<string>();
  187. }
  188. }
  189. }
  190. }
  191. }