SHttpUtil.kt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * iFHS7.
  5. * ;BBMBMBMc rZMBMBR BMB
  6. * MBEr:;PBM, 7MBMMEOBB: BBB RBW
  7. * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2;
  8. * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB
  9. * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv
  10. * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc
  11. * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7
  12. * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2
  13. * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO;
  14. * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB
  15. * :0BMRDG RESSSKR. 2WOMBW; BMBMR
  16. * i0BM: SWKHKGO MBDv
  17. * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人
  18. * , XMW ..
  19. * r All rights reserved.
  20. *
  21. * ********************************************************************************************************************
  22. */
  23. package com.sybotan.base.utils
  24. import com.alibaba.fastjson.PropertyNamingStrategy
  25. import com.sybotan.base.extensions.toJson
  26. import okhttp3.*
  27. import java.util.concurrent.TimeUnit
  28. /**
  29. * Http工具类
  30. *
  31. * @author 庞利祥 <sybotan@126.com>
  32. */
  33. object SHttpUtil {
  34. /** 连接超时时间 */
  35. var connectTimeout: Long = 30L
  36. /** 读取超时时间 */
  37. var readTimeout: Long = 30L
  38. /** 写入超时时间 */
  39. var writeTimeout: Long = 30L
  40. /** http客户端 */
  41. private val httpClient: OkHttpClient by lazy {
  42. OkHttpClient.Builder()
  43. .connectTimeout(connectTimeout, TimeUnit.SECONDS)
  44. .readTimeout(readTimeout, TimeUnit.SECONDS)
  45. .writeTimeout(writeTimeout, TimeUnit.SECONDS)
  46. .build()
  47. }
  48. /** 媒体类型 */
  49. val ALTERNATIVE = MediaType.parse("multipart/alternative")
  50. val DIGEST = MediaType.parse("multipart/digest")
  51. val FORM = MediaType.parse("multipart/form-data")
  52. val JSON = MediaType.parse("application/json; charset=utf-8")
  53. val MIXED = MediaType.parse("multipart/mixed")
  54. val PARALLEL = MediaType.parse("multipart/parallel")
  55. /**
  56. * 根据扩展名获得Http的Content-Type
  57. *
  58. * @param extensionName 文件扩展名
  59. * @return 扩展名对应的Content-Type
  60. */
  61. fun contentType(extensionName: String): String {
  62. val map = mapOf(
  63. "tif" to "image/tiff",
  64. "001" to "application/x-001",
  65. "301" to "application/x-301",
  66. "323" to "text/h323",
  67. "906" to "application/x-906",
  68. "907" to "drawing/907",
  69. "a11" to "application/x-a11",
  70. "acp" to "audio/x-mei-aac",
  71. "ai" to "application/postscript",
  72. "aif" to "audio/aiff",
  73. "aifc" to "audio/aiff",
  74. "aiff" to "audio/aiff",
  75. "anv" to "application/x-anv",
  76. "asa" to "text/asa",
  77. "asf" to "video/x-ms-asf",
  78. "asp" to "text/asp",
  79. "asx" to "video/x-ms-asf",
  80. "au" to "audio/basic",
  81. "avi" to "video/avi",
  82. "awf" to "application/vnd.adobe.workflow",
  83. "biz" to "text/xml",
  84. "bmp" to "application/x-bmp",
  85. "bot" to "application/x-bot",
  86. "c4t" to "application/x-c4t",
  87. "c90" to "application/x-c90",
  88. "cal" to "application/x-cals",
  89. "cat" to "application/vnd.ms-pki.seccat",
  90. "cdf" to "application/x-netcdf",
  91. "cdr" to "application/x-cdr",
  92. "cel" to "application/x-cel",
  93. "cer" to "application/x-x509-ca-cert",
  94. "cg4" to "application/x-g4",
  95. "cgm" to "application/x-cgm",
  96. "cit" to "application/x-cit",
  97. "class" to "java/*",
  98. "cml" to "text/xml",
  99. "cmp" to "application/x-cmp",
  100. "cmx" to "application/x-cmx",
  101. "cot" to "application/x-cot",
  102. "crl" to "application/pkix-crl",
  103. "crt" to "application/x-x509-ca-cert",
  104. "csi" to "application/x-csi",
  105. "css" to "text/css",
  106. "cut" to "application/x-cut",
  107. "dbf" to "application/x-dbf",
  108. "dbm" to "application/x-dbm",
  109. "dbx" to "application/x-dbx",
  110. "dcd" to "text/xml",
  111. "dcx" to "application/x-dcx",
  112. "der" to "application/x-x509-ca-cert",
  113. "dgn" to "application/x-dgn",
  114. "dib" to "application/x-dib",
  115. "dll" to "application/x-msdownload",
  116. "doc" to "application/msword",
  117. "dot" to "application/msword",
  118. "drw" to "application/x-drw",
  119. "dtd" to "text/xml",
  120. "dwf" to "Model/vnd.dwf",
  121. "dwf" to "application/x-dwf",
  122. "dwg" to "application/x-dwg",
  123. "dxb" to "application/x-dxb",
  124. "dxf" to "application/x-dxf",
  125. "edn" to "application/vnd.adobe.edn",
  126. "emf" to "application/x-emf",
  127. "eml" to "message/rfc822",
  128. "ent" to "text/xml",
  129. "epi" to "application/x-epi",
  130. "eps" to "application/x-ps",
  131. "eps" to "application/postscript",
  132. "etd" to "application/x-ebx",
  133. "exe" to "application/x-msdownload",
  134. "fax" to "image/fax",
  135. "fdf" to "application/vnd.fdf",
  136. "fif" to "application/fractals",
  137. "fo" to "text/xml",
  138. "frm" to "application/x-frm",
  139. "g4" to "application/x-g4",
  140. "gbr" to "application/x-gbr",
  141. "gif" to "image/gif",
  142. "gl2" to "application/x-gl2",
  143. "gp4" to "application/x-gp4",
  144. "hgl" to "application/x-hgl",
  145. "hmr" to "application/x-hmr",
  146. "hpg" to "application/x-hpgl",
  147. "hpl" to "application/x-hpl",
  148. "hqx" to "application/mac-binhex40",
  149. "hrf" to "application/x-hrf",
  150. "hta" to "application/hta",
  151. "htc" to "text/x-component",
  152. "htm" to "text/html",
  153. "html" to "text/html",
  154. "htt" to "text/webviewhtml",
  155. "htx" to "text/html",
  156. "icb" to "application/x-icb",
  157. "ico" to "image/x-icon",
  158. "ico" to "application/x-ico",
  159. "iff" to "application/x-iff",
  160. "ig4" to "application/x-g4",
  161. "igs" to "application/x-igs",
  162. "iii" to "application/x-iphone",
  163. "img" to "application/x-img",
  164. "ins" to "application/x-internet-signup",
  165. "isp" to "application/x-internet-signup",
  166. "ivf" to "video/x-ivf",
  167. "java" to "java/*",
  168. "jfif" to "image/jpeg",
  169. "jpe" to "image/jpeg",
  170. "jpe" to "application/x-jpe",
  171. "jpeg" to "image/jpeg",
  172. "jpg" to "image/jpeg",
  173. "jpg" to "application/x-jpg",
  174. "js" to "application/x-javascript",
  175. "jsp" to "text/html",
  176. "la1" to "audio/x-liquid-resources",
  177. "lar" to "application/x-laplayer-reg",
  178. "latex" to "application/x-latex",
  179. "lavs" to "audio/x-liquid-secure",
  180. "lbm" to "application/x-lbm",
  181. "lmsff" to "audio/x-la-lms",
  182. "ls" to "application/x-javascript",
  183. "ltr" to "application/x-ltr",
  184. "m1v" to "video/x-mpeg",
  185. "m2v" to "video/x-mpeg",
  186. "m3u" to "audio/mpegurl",
  187. "m4e" to "video/mpeg4",
  188. "mac" to "application/x-mac",
  189. "man" to "application/x-troff-man",
  190. "math" to "text/xml",
  191. "mdb" to "application/msaccess",
  192. "mdb" to "application/x-mdb",
  193. "mfp" to "application/x-shockwave-flash",
  194. "mht" to "message/rfc822",
  195. "mhtml" to "message/rfc822",
  196. "mi" to "application/x-mi",
  197. "mid" to "audio/mid",
  198. "midi" to "audio/mid",
  199. "mil" to "application/x-mil",
  200. "mml" to "text/xml",
  201. "mnd" to "audio/x-musicnet-download",
  202. "mns" to "audio/x-musicnet-stream",
  203. "mocha" to "application/x-javascript",
  204. "movie" to "video/x-sgi-movie",
  205. "mp1" to "audio/mp1",
  206. "mp2" to "audio/mp2",
  207. "mp2v" to "video/mpeg",
  208. "mp3" to "audio/mp3",
  209. "mp4" to "video/mp4",
  210. "mpa" to "video/x-mpg",
  211. "mpd" to "application/vnd.ms-project",
  212. "mpe" to "video/x-mpeg",
  213. "mpeg" to "video/mpg",
  214. "mpg" to "video/mpg",
  215. "mpga" to "audio/rn-mpeg",
  216. "mpp" to "application/vnd.ms-project",
  217. "mps" to "video/x-mpeg",
  218. "mpt" to "application/vnd.ms-project",
  219. "mpv" to "video/mpg",
  220. "mpv2" to "video/mpeg",
  221. "mpw" to "application/vnd.ms-project",
  222. "mpx" to "application/vnd.ms-project",
  223. "mtx" to "text/xml",
  224. "mxp" to "application/x-mmxp",
  225. "net" to "image/pnetvue",
  226. "nrf" to "application/x-nrf",
  227. "nws" to "message/rfc822",
  228. "odc" to "text/x-ms-odc",
  229. "out" to "application/x-out",
  230. "p10" to "application/pkcs10",
  231. "p12" to "application/x-pkcs12",
  232. "p7b" to "application/x-pkcs7-certificates",
  233. "p7c" to "application/pkcs7-mime",
  234. "p7m" to "application/pkcs7-mime",
  235. "p7r" to "application/x-pkcs7-certreqresp",
  236. "p7s" to "application/pkcs7-signature",
  237. "pc5" to "application/x-pc5",
  238. "pci" to "application/x-pci",
  239. "pcl" to "application/x-pcl",
  240. "pcx" to "application/x-pcx",
  241. "pdf" to "application/pdf",
  242. "pdx" to "application/vnd.adobe.pdx",
  243. "pfx" to "application/x-pkcs12",
  244. "pgl" to "application/x-pgl",
  245. "pic" to "application/x-pic",
  246. "pko" to "application/vnd.ms-pki.pko",
  247. "pl" to "application/x-perl",
  248. "plg" to "text/html",
  249. "pls" to "audio/scpls",
  250. "plt" to "application/x-plt",
  251. "png" to "image/png",
  252. "png" to "application/x-png",
  253. "pot" to "application/vnd.ms-powerpoint",
  254. "ppa" to "application/vnd.ms-powerpoint",
  255. "ppm" to "application/x-ppm",
  256. "pps" to "application/vnd.ms-powerpoint",
  257. "ppt" to "application/vnd.ms-powerpoint",
  258. "ppt" to "application/x-ppt",
  259. "pr" to "application/x-pr",
  260. "prf" to "application/pics-rules",
  261. "prn" to "application/x-prn",
  262. "prt" to "application/x-prt",
  263. "ps" to "application/x-ps",
  264. "ptn" to "application/x-ptn",
  265. "pwz" to "application/vnd.ms-powerpoint",
  266. "r3t" to "text/vnd.rn-realtext3d",
  267. "ra" to "audio/vnd.rn-realaudio",
  268. "ram" to "audio/x-pn-realaudio",
  269. "ras" to "application/x-ras",
  270. "rat" to "application/rat-resources",
  271. "rdf" to "text/xml",
  272. "rec" to "application/vnd.rn-recording",
  273. "red" to "application/x-red",
  274. "rgb" to "application/x-rgb",
  275. "rjs" to "application/vnd.rn-realsystem-rjs",
  276. "rjt" to "application/vnd.rn-realsystem-rjt",
  277. "rlc" to "application/x-rlc",
  278. "rle" to "application/x-rle",
  279. "rm" to "application/vnd.rn-realmedia",
  280. "rmf" to "application/vnd.adobe.rmf",
  281. "rmi" to "audio/mid",
  282. "rmj" to "application/vnd.rn-realsystem-rmj",
  283. "rmm" to "audio/x-pn-realaudio",
  284. "rmp" to "application/vnd.rn-rn_music_package",
  285. "rms" to "application/vnd.rn-realmedia-secure",
  286. "rmvb" to "application/vnd.rn-realmedia-vbr",
  287. "rmx" to "application/vnd.rn-realsystem-rmx",
  288. "rnx" to "application/vnd.rn-realplayer",
  289. "rp" to "image/vnd.rn-realpix",
  290. "rpm" to "audio/x-pn-realaudio-plugin",
  291. "rsml" to "application/vnd.rn-rsml",
  292. "rt" to "text/vnd.rn-realtext",
  293. "rtf" to "application/msword",
  294. "rtf" to "application/x-rtf",
  295. "rv" to "video/vnd.rn-realvideo",
  296. "sam" to "application/x-sam",
  297. "sat" to "application/x-sat",
  298. "sdp" to "application/sdp",
  299. "sdw" to "application/x-sdw",
  300. "sit" to "application/x-stuffit",
  301. "slb" to "application/x-slb",
  302. "sld" to "application/x-sld",
  303. "slk" to "drawing/x-slk",
  304. "smi" to "application/smil",
  305. "smil" to "application/smil",
  306. "smk" to "application/x-smk",
  307. "snd" to "audio/basic",
  308. "sol" to "text/plain",
  309. "sor" to "text/plain",
  310. "spc" to "application/x-pkcs7-certificates",
  311. "spl" to "application/futuresplash",
  312. "spp" to "text/xml",
  313. "ssm" to "application/streamingmedia",
  314. "sst" to "application/vnd.ms-pki.certs tore",
  315. "stl" to "application/vnd.ms-pki.stl",
  316. "stm" to "text/html",
  317. "sty" to "application/x-sty",
  318. "svg" to "text/xml",
  319. "swf" to "application/x-shockwave-flash",
  320. "tdf" to "application/x-tdf",
  321. "tg4" to "application/x-tg4",
  322. "tga" to "application/x-tga",
  323. "tif" to "image/tiff",
  324. "tif" to "application/x-tif",
  325. "tiff" to "image/tiff",
  326. "tld" to "text/xml",
  327. "top" to "drawing/x-top",
  328. "torrent" to "application/x-bittorrent",
  329. "tsd" to "text/xml",
  330. "txt" to "text/plain",
  331. "uin" to "application/x-icq",
  332. "uls" to "text/iuls",
  333. "vcf" to "text/x-vcard",
  334. "vda" to "application/x-vda",
  335. "vdx" to "application/vnd.visio",
  336. "vml" to "text/xml",
  337. "vpg" to "application/x-vpeg005",
  338. "vsd" to "application/vnd.visio",
  339. "vsd" to "application/x-vsd",
  340. "vss" to "application/vnd.visio",
  341. "vst" to "application/vnd.visio",
  342. "vst" to "application/x-vst",
  343. "vsw" to "application/vnd.visio",
  344. "vsx" to "application/vnd.visio",
  345. "vtx" to "application/vnd.visio",
  346. "vxml" to "text/xml",
  347. "wav" to "audio/wav",
  348. "wax" to "audio/x-ms-wax",
  349. "wb1" to "application/x-wb1",
  350. "wb2" to "application/x-wb2",
  351. "wb3" to "application/x-wb3",
  352. "wbmp" to "image/vnd.wap.wbmp",
  353. "wiz" to "application/msword",
  354. "wk3" to "application/x-wk3",
  355. "wk4" to "application/x-wk4",
  356. "wkq" to "application/x-wkq",
  357. "wks" to "application/x-wks",
  358. "wm" to "video/x-ms-wm",
  359. "wma" to "audio/x-ms-wma",
  360. "wmd" to "application/x-ms-wmd",
  361. "wmf" to "application/x-wmf",
  362. "wml" to "text/vnd.wap.wml",
  363. "wmv" to "video/x-ms-wmv",
  364. "wmx" to "video/x-ms-wmx",
  365. "wmz" to "application/x-ms-wmz",
  366. "wp6" to "application/x-wp6",
  367. "wpd" to "application/x-wpd",
  368. "wpg" to "application/x-wpg",
  369. "wpl" to "application/vnd.ms-wpl",
  370. "wq1" to "application/x-wq1",
  371. "wr1" to "application/x-wr1",
  372. "wri" to "application/x-wri",
  373. "wrk" to "application/x-wrk",
  374. "ws" to "application/x-ws",
  375. "ws2" to "application/x-ws",
  376. "wsc" to "text/scriptlet",
  377. "wsdl" to "text/xml",
  378. "wvx" to "video/x-ms-wvx",
  379. "xdp" to "application/vnd.adobe.xdp",
  380. "xdr" to "text/xml",
  381. "xfd" to "application/vnd.adobe.xfd",
  382. "xfdf" to "application/vnd.adobe.xfdf",
  383. "xhtml" to "text/html",
  384. "xls" to "application/vnd.ms-excel",
  385. "xls" to "application/x-xls",
  386. "xlw" to "application/x-xlw",
  387. "xml" to "text/xml",
  388. "xpl" to "audio/scpls",
  389. "xq" to "text/xml",
  390. "xql" to "text/xml",
  391. "xquery" to "text/xml",
  392. "xsd" to "text/xml",
  393. "xsl" to "text/xml",
  394. "xslt" to "text/xml",
  395. "xwd" to "application/x-xwd",
  396. "x_b" to "application/x-x_b",
  397. "sis" to "application/vnd.symbian.install",
  398. "sisx" to "application/vnd.symbian.install",
  399. "x_t" to "application/x-x_t",
  400. "ipa" to "application/vnd.iphone",
  401. "apk" to "application/vnd.android.package-archive",
  402. "xap" to "application/x-silverlight-app"
  403. )
  404. // 扩展名转换为小写
  405. val key = extensionName.toLowerCase()
  406. // 如果map包含指定的扩展名,则返回对应的Content-Type, 否则返回二进制流类型"application/octet-stream"
  407. return if (map.contains(key)) {
  408. map.getValue(key)
  409. } else {
  410. "application/octet-stream"
  411. }
  412. } // Fun contentType()
  413. /**
  414. * GET 请求
  415. *
  416. * @param url URL 地址
  417. * @return 服务器返回的应答信息
  418. */
  419. @Throws(Exception::class)
  420. fun getRequest(url: String): String {
  421. val request = Request.Builder()
  422. .url(url)
  423. .get()
  424. .build()
  425. val call = httpClient.newCall(request)
  426. val response = call.execute()
  427. return response.body()!!.string()
  428. } // Fun getRequest()
  429. /**
  430. * GET 请求(zip压缩格式)
  431. *
  432. * @param url URL地址
  433. * @return 服务器返回的应答信息
  434. */
  435. @Throws(Exception::class)
  436. fun getZipRequest(url: String): String {
  437. val request = Request.Builder()
  438. .url(url)
  439. .get()
  440. .header("Accept-Encoding", "gzip")
  441. .build()
  442. val call = httpClient.newCall(request)
  443. val response = call.execute()
  444. return SGzipUtil.uncompressToString(response.body()!!.bytes()) ?: ""
  445. } // Fun getZipRequest()
  446. /**
  447. * 发送post对象请求
  448. *
  449. * @param url URL地址
  450. * @param body 请求体
  451. * @return 返回应答体字符串
  452. */
  453. @Throws(Exception::class)
  454. fun postRequest(url: String, body: String, type: MediaType = JSON!!): String {
  455. val jsonBody = FormBody.create(type, body)
  456. val request = createPostRequest(url, jsonBody)
  457. val call = httpClient.newCall(request)
  458. val response = call.execute()
  459. return response.body()!!.string()
  460. } // Fun postRequest()
  461. /**
  462. * 发送get对象请求
  463. *
  464. * @param <T> 请求的返回类型
  465. * @param url URL地址
  466. * @param namingStrategy 命名规则
  467. * @return 接收到的请求体
  468. */
  469. @Throws(Exception::class)
  470. inline fun <reified T> getObject(url: String, namingStrategy: PropertyNamingStrategy? = null): T {
  471. val responseJson = getRequest(url)
  472. return SJsonUtil.fromJson(responseJson, namingStrategy)
  473. } // Fun getObject()
  474. /**
  475. * 发送post请求
  476. *
  477. * @param url URL地址
  478. * @param <T> 请求的返回类型
  479. * @param requestBody 发送到服务器的请求体
  480. * @param namingStrategy 命名规则
  481. * @return 接收到的请求体
  482. */
  483. @Throws(Exception::class)
  484. inline fun <reified T> postObject(url: String, requestBody: Any, namingStrategy: PropertyNamingStrategy? = null): T {
  485. val responseJson = postRequest(url, requestBody.toJson(namingStrategy))
  486. return SJsonUtil.fromJson(responseJson, namingStrategy)
  487. } // Fun postObject()
  488. /**
  489. * 上传文件
  490. *
  491. * @param url 上传地址
  492. */
  493. fun uploadFile(url: String) {
  494. /*RequestBody requestBody = new MultipartBody.Builder()
  495. .setType(MultipartBody.FORM)
  496. .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
  497. .build();*/
  498. return
  499. } // Fun uploadFile()
  500. /**
  501. * 下载文件
  502. *
  503. * @param url 文件url地址
  504. */
  505. fun downloadFile(url: String) {
  506. } // Fun downloadFile()
  507. /**
  508. * 创建Http post请求对象
  509. *
  510. * @param url URL地址
  511. * @param jsonBody 发送到服务器的请求体
  512. */
  513. private fun createPostRequest(url: String, jsonBody: RequestBody? = null): Request {
  514. val builder = Request.Builder()
  515. .url(url)
  516. if (jsonBody!= null) {
  517. builder.post(jsonBody)
  518. }
  519. return builder.build()
  520. } // Fun createPostRequest()
  521. } // Object SHttpUtil