fun getUrl(): String? {
val okHttpClient = OkHttpClient()
val url = "https://www.sofreight.com/Tools/trackingSearch.html";
val mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
val postData = "bl_no=A324324234666d&ctnr_no=&carrier_code=APL";
val postbody: RequestBody = RequestBody.create(mediaType, postData)
val body = url?.let { Request.Builder().url(it).post(postbody) }
?.let { it.header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8") }
?.let { it.build() }
?.let { okHttpClient.newCall(it).execute() }
?.let { getBodyString(it) }
println(body)
return body ?: "emtpy"
}
private fun getBodyString(it: Response) = String(it.body().bytes(), Charset.forName("utf-8"))
Kotlin下使用Okhttpclient访问Url
最新推荐文章于 2024-06-07 16:14:56 发布