/* * ******************************************************************************************************************** * * iFHS7. * ;BBMBMBMc rZMBMBR BMB * MBEr:;PBM, 7MBMMEOBB: BBB RBW * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2; * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7 * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2 * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO; * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB * :0BMRDG RESSSKR. 2WOMBW; BMBMR * i0BM: SWKHKGO MBDv * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人 * , XMW .. * r All rights reserved. * * ******************************************************************************************************************** */ package com.sybotan.base.utils import com.alibaba.fastjson.PropertyNamingStrategy import com.sybotan.base.extensions.toJson import okhttp3.* import java.util.concurrent.TimeUnit /** * Http工具类 * * @author 庞利祥 */ object SHttpUtil { /** 连接超时时间 */ var connectTimeout: Long = 30L /** 读取超时时间 */ var readTimeout: Long = 30L /** 写入超时时间 */ var writeTimeout: Long = 30L /** http客户端 */ private val httpClient: OkHttpClient by lazy { OkHttpClient.Builder() .connectTimeout(connectTimeout, TimeUnit.SECONDS) .readTimeout(readTimeout, TimeUnit.SECONDS) .writeTimeout(writeTimeout, TimeUnit.SECONDS) .build() } /** 媒体类型 */ val ALTERNATIVE = MediaType.parse("multipart/alternative") val DIGEST = MediaType.parse("multipart/digest") val FORM = MediaType.parse("multipart/form-data") val JSON = MediaType.parse("application/json; charset=utf-8") val MIXED = MediaType.parse("multipart/mixed") val PARALLEL = MediaType.parse("multipart/parallel") /** * 根据扩展名获得Http的Content-Type * * @param extensionName 文件扩展名 * @return 扩展名对应的Content-Type */ fun contentType(extensionName: String): String { val map = mapOf( "tif" to "image/tiff", "001" to "application/x-001", "301" to "application/x-301", "323" to "text/h323", "906" to "application/x-906", "907" to "drawing/907", "a11" to "application/x-a11", "acp" to "audio/x-mei-aac", "ai" to "application/postscript", "aif" to "audio/aiff", "aifc" to "audio/aiff", "aiff" to "audio/aiff", "anv" to "application/x-anv", "asa" to "text/asa", "asf" to "video/x-ms-asf", "asp" to "text/asp", "asx" to "video/x-ms-asf", "au" to "audio/basic", "avi" to "video/avi", "awf" to "application/vnd.adobe.workflow", "biz" to "text/xml", "bmp" to "application/x-bmp", "bot" to "application/x-bot", "c4t" to "application/x-c4t", "c90" to "application/x-c90", "cal" to "application/x-cals", "cat" to "application/vnd.ms-pki.seccat", "cdf" to "application/x-netcdf", "cdr" to "application/x-cdr", "cel" to "application/x-cel", "cer" to "application/x-x509-ca-cert", "cg4" to "application/x-g4", "cgm" to "application/x-cgm", "cit" to "application/x-cit", "class" to "java/*", "cml" to "text/xml", "cmp" to "application/x-cmp", "cmx" to "application/x-cmx", "cot" to "application/x-cot", "crl" to "application/pkix-crl", "crt" to "application/x-x509-ca-cert", "csi" to "application/x-csi", "css" to "text/css", "cut" to "application/x-cut", "dbf" to "application/x-dbf", "dbm" to "application/x-dbm", "dbx" to "application/x-dbx", "dcd" to "text/xml", "dcx" to "application/x-dcx", "der" to "application/x-x509-ca-cert", "dgn" to "application/x-dgn", "dib" to "application/x-dib", "dll" to "application/x-msdownload", "doc" to "application/msword", "dot" to "application/msword", "drw" to "application/x-drw", "dtd" to "text/xml", "dwf" to "Model/vnd.dwf", "dwf" to "application/x-dwf", "dwg" to "application/x-dwg", "dxb" to "application/x-dxb", "dxf" to "application/x-dxf", "edn" to "application/vnd.adobe.edn", "emf" to "application/x-emf", "eml" to "message/rfc822", "ent" to "text/xml", "epi" to "application/x-epi", "eps" to "application/x-ps", "eps" to "application/postscript", "etd" to "application/x-ebx", "exe" to "application/x-msdownload", "fax" to "image/fax", "fdf" to "application/vnd.fdf", "fif" to "application/fractals", "fo" to "text/xml", "frm" to "application/x-frm", "g4" to "application/x-g4", "gbr" to "application/x-gbr", "gif" to "image/gif", "gl2" to "application/x-gl2", "gp4" to "application/x-gp4", "hgl" to "application/x-hgl", "hmr" to "application/x-hmr", "hpg" to "application/x-hpgl", "hpl" to "application/x-hpl", "hqx" to "application/mac-binhex40", "hrf" to "application/x-hrf", "hta" to "application/hta", "htc" to "text/x-component", "htm" to "text/html", "html" to "text/html", "htt" to "text/webviewhtml", "htx" to "text/html", "icb" to "application/x-icb", "ico" to "image/x-icon", "ico" to "application/x-ico", "iff" to "application/x-iff", "ig4" to "application/x-g4", "igs" to "application/x-igs", "iii" to "application/x-iphone", "img" to "application/x-img", "ins" to "application/x-internet-signup", "isp" to "application/x-internet-signup", "ivf" to "video/x-ivf", "java" to "java/*", "jfif" to "image/jpeg", "jpe" to "image/jpeg", "jpe" to "application/x-jpe", "jpeg" to "image/jpeg", "jpg" to "image/jpeg", "jpg" to "application/x-jpg", "js" to "application/x-javascript", "jsp" to "text/html", "la1" to "audio/x-liquid-resources", "lar" to "application/x-laplayer-reg", "latex" to "application/x-latex", "lavs" to "audio/x-liquid-secure", "lbm" to "application/x-lbm", "lmsff" to "audio/x-la-lms", "ls" to "application/x-javascript", "ltr" to "application/x-ltr", "m1v" to "video/x-mpeg", "m2v" to "video/x-mpeg", "m3u" to "audio/mpegurl", "m4e" to "video/mpeg4", "mac" to "application/x-mac", "man" to "application/x-troff-man", "math" to "text/xml", "mdb" to "application/msaccess", "mdb" to "application/x-mdb", "mfp" to "application/x-shockwave-flash", "mht" to "message/rfc822", "mhtml" to "message/rfc822", "mi" to "application/x-mi", "mid" to "audio/mid", "midi" to "audio/mid", "mil" to "application/x-mil", "mml" to "text/xml", "mnd" to "audio/x-musicnet-download", "mns" to "audio/x-musicnet-stream", "mocha" to "application/x-javascript", "movie" to "video/x-sgi-movie", "mp1" to "audio/mp1", "mp2" to "audio/mp2", "mp2v" to "video/mpeg", "mp3" to "audio/mp3", "mp4" to "video/mp4", "mpa" to "video/x-mpg", "mpd" to "application/vnd.ms-project", "mpe" to "video/x-mpeg", "mpeg" to "video/mpg", "mpg" to "video/mpg", "mpga" to "audio/rn-mpeg", "mpp" to "application/vnd.ms-project", "mps" to "video/x-mpeg", "mpt" to "application/vnd.ms-project", "mpv" to "video/mpg", "mpv2" to "video/mpeg", "mpw" to "application/vnd.ms-project", "mpx" to "application/vnd.ms-project", "mtx" to "text/xml", "mxp" to "application/x-mmxp", "net" to "image/pnetvue", "nrf" to "application/x-nrf", "nws" to "message/rfc822", "odc" to "text/x-ms-odc", "out" to "application/x-out", "p10" to "application/pkcs10", "p12" to "application/x-pkcs12", "p7b" to "application/x-pkcs7-certificates", "p7c" to "application/pkcs7-mime", "p7m" to "application/pkcs7-mime", "p7r" to "application/x-pkcs7-certreqresp", "p7s" to "application/pkcs7-signature", "pc5" to "application/x-pc5", "pci" to "application/x-pci", "pcl" to "application/x-pcl", "pcx" to "application/x-pcx", "pdf" to "application/pdf", "pdx" to "application/vnd.adobe.pdx", "pfx" to "application/x-pkcs12", "pgl" to "application/x-pgl", "pic" to "application/x-pic", "pko" to "application/vnd.ms-pki.pko", "pl" to "application/x-perl", "plg" to "text/html", "pls" to "audio/scpls", "plt" to "application/x-plt", "png" to "image/png", "png" to "application/x-png", "pot" to "application/vnd.ms-powerpoint", "ppa" to "application/vnd.ms-powerpoint", "ppm" to "application/x-ppm", "pps" to "application/vnd.ms-powerpoint", "ppt" to "application/vnd.ms-powerpoint", "ppt" to "application/x-ppt", "pr" to "application/x-pr", "prf" to "application/pics-rules", "prn" to "application/x-prn", "prt" to "application/x-prt", "ps" to "application/x-ps", "ptn" to "application/x-ptn", "pwz" to "application/vnd.ms-powerpoint", "r3t" to "text/vnd.rn-realtext3d", "ra" to "audio/vnd.rn-realaudio", "ram" to "audio/x-pn-realaudio", "ras" to "application/x-ras", "rat" to "application/rat-resources", "rdf" to "text/xml", "rec" to "application/vnd.rn-recording", "red" to "application/x-red", "rgb" to "application/x-rgb", "rjs" to "application/vnd.rn-realsystem-rjs", "rjt" to "application/vnd.rn-realsystem-rjt", "rlc" to "application/x-rlc", "rle" to "application/x-rle", "rm" to "application/vnd.rn-realmedia", "rmf" to "application/vnd.adobe.rmf", "rmi" to "audio/mid", "rmj" to "application/vnd.rn-realsystem-rmj", "rmm" to "audio/x-pn-realaudio", "rmp" to "application/vnd.rn-rn_music_package", "rms" to "application/vnd.rn-realmedia-secure", "rmvb" to "application/vnd.rn-realmedia-vbr", "rmx" to "application/vnd.rn-realsystem-rmx", "rnx" to "application/vnd.rn-realplayer", "rp" to "image/vnd.rn-realpix", "rpm" to "audio/x-pn-realaudio-plugin", "rsml" to "application/vnd.rn-rsml", "rt" to "text/vnd.rn-realtext", "rtf" to "application/msword", "rtf" to "application/x-rtf", "rv" to "video/vnd.rn-realvideo", "sam" to "application/x-sam", "sat" to "application/x-sat", "sdp" to "application/sdp", "sdw" to "application/x-sdw", "sit" to "application/x-stuffit", "slb" to "application/x-slb", "sld" to "application/x-sld", "slk" to "drawing/x-slk", "smi" to "application/smil", "smil" to "application/smil", "smk" to "application/x-smk", "snd" to "audio/basic", "sol" to "text/plain", "sor" to "text/plain", "spc" to "application/x-pkcs7-certificates", "spl" to "application/futuresplash", "spp" to "text/xml", "ssm" to "application/streamingmedia", "sst" to "application/vnd.ms-pki.certs tore", "stl" to "application/vnd.ms-pki.stl", "stm" to "text/html", "sty" to "application/x-sty", "svg" to "text/xml", "swf" to "application/x-shockwave-flash", "tdf" to "application/x-tdf", "tg4" to "application/x-tg4", "tga" to "application/x-tga", "tif" to "image/tiff", "tif" to "application/x-tif", "tiff" to "image/tiff", "tld" to "text/xml", "top" to "drawing/x-top", "torrent" to "application/x-bittorrent", "tsd" to "text/xml", "txt" to "text/plain", "uin" to "application/x-icq", "uls" to "text/iuls", "vcf" to "text/x-vcard", "vda" to "application/x-vda", "vdx" to "application/vnd.visio", "vml" to "text/xml", "vpg" to "application/x-vpeg005", "vsd" to "application/vnd.visio", "vsd" to "application/x-vsd", "vss" to "application/vnd.visio", "vst" to "application/vnd.visio", "vst" to "application/x-vst", "vsw" to "application/vnd.visio", "vsx" to "application/vnd.visio", "vtx" to "application/vnd.visio", "vxml" to "text/xml", "wav" to "audio/wav", "wax" to "audio/x-ms-wax", "wb1" to "application/x-wb1", "wb2" to "application/x-wb2", "wb3" to "application/x-wb3", "wbmp" to "image/vnd.wap.wbmp", "wiz" to "application/msword", "wk3" to "application/x-wk3", "wk4" to "application/x-wk4", "wkq" to "application/x-wkq", "wks" to "application/x-wks", "wm" to "video/x-ms-wm", "wma" to "audio/x-ms-wma", "wmd" to "application/x-ms-wmd", "wmf" to "application/x-wmf", "wml" to "text/vnd.wap.wml", "wmv" to "video/x-ms-wmv", "wmx" to "video/x-ms-wmx", "wmz" to "application/x-ms-wmz", "wp6" to "application/x-wp6", "wpd" to "application/x-wpd", "wpg" to "application/x-wpg", "wpl" to "application/vnd.ms-wpl", "wq1" to "application/x-wq1", "wr1" to "application/x-wr1", "wri" to "application/x-wri", "wrk" to "application/x-wrk", "ws" to "application/x-ws", "ws2" to "application/x-ws", "wsc" to "text/scriptlet", "wsdl" to "text/xml", "wvx" to "video/x-ms-wvx", "xdp" to "application/vnd.adobe.xdp", "xdr" to "text/xml", "xfd" to "application/vnd.adobe.xfd", "xfdf" to "application/vnd.adobe.xfdf", "xhtml" to "text/html", "xls" to "application/vnd.ms-excel", "xls" to "application/x-xls", "xlw" to "application/x-xlw", "xml" to "text/xml", "xpl" to "audio/scpls", "xq" to "text/xml", "xql" to "text/xml", "xquery" to "text/xml", "xsd" to "text/xml", "xsl" to "text/xml", "xslt" to "text/xml", "xwd" to "application/x-xwd", "x_b" to "application/x-x_b", "sis" to "application/vnd.symbian.install", "sisx" to "application/vnd.symbian.install", "x_t" to "application/x-x_t", "ipa" to "application/vnd.iphone", "apk" to "application/vnd.android.package-archive", "xap" to "application/x-silverlight-app" ) // 扩展名转换为小写 val key = extensionName.toLowerCase() // 如果map包含指定的扩展名,则返回对应的Content-Type, 否则返回二进制流类型"application/octet-stream" return if (map.contains(key)) { map.getValue(key) } else { "application/octet-stream" } } // Fun contentType() /** * GET 请求 * * @param url URL 地址 * @return 服务器返回的应答信息 */ @Throws(Exception::class) fun getRequest(url: String): String { val request = Request.Builder() .url(url) .get() .build() val call = httpClient.newCall(request) val response = call.execute() return response.body()!!.string() } // Fun getRequest() /** * GET 请求(zip压缩格式) * * @param url URL地址 * @return 服务器返回的应答信息 */ @Throws(Exception::class) fun getZipRequest(url: String): String { val request = Request.Builder() .url(url) .get() .header("Accept-Encoding", "gzip") .build() val call = httpClient.newCall(request) val response = call.execute() return SGzipUtil.uncompressToString(response.body()!!.bytes()) ?: "" } // Fun getZipRequest() /** * 发送post对象请求 * * @param url URL地址 * @param body 请求体 * @return 返回应答体字符串 */ @Throws(Exception::class) fun postRequest(url: String, body: String, type: MediaType = JSON!!): String { val jsonBody = FormBody.create(type, body) val request = createPostRequest(url, jsonBody) val call = httpClient.newCall(request) val response = call.execute() return response.body()!!.string() } // Fun postRequest() /** * 发送get对象请求 * * @param 请求的返回类型 * @param url URL地址 * @param namingStrategy 命名规则 * @return 接收到的请求体 */ @Throws(Exception::class) inline fun getObject(url: String, namingStrategy: PropertyNamingStrategy? = null): T { val responseJson = getRequest(url) return SJsonUtil.fromJson(responseJson, namingStrategy) } // Fun getObject() /** * 发送post请求 * * @param url URL地址 * @param 请求的返回类型 * @param requestBody 发送到服务器的请求体 * @param namingStrategy 命名规则 * @return 接收到的请求体 */ @Throws(Exception::class) inline fun postObject(url: String, requestBody: Any, namingStrategy: PropertyNamingStrategy? = null): T { val responseJson = postRequest(url, requestBody.toJson(namingStrategy)) return SJsonUtil.fromJson(responseJson, namingStrategy) } // Fun postObject() /** * 上传文件 * * @param url 上传地址 */ fun uploadFile(url: String) { /*RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file)) .build();*/ return } // Fun uploadFile() /** * 下载文件 * * @param url 文件url地址 */ fun downloadFile(url: String) { } // Fun downloadFile() /** * 创建Http post请求对象 * * @param url URL地址 * @param jsonBody 发送到服务器的请求体 */ private fun createPostRequest(url: String, jsonBody: RequestBody? = null): Request { val builder = Request.Builder() .url(url) if (jsonBody!= null) { builder.post(jsonBody) } return builder.build() } // Fun createPostRequest() } // Object SHttpUtil