package com.flightfeather.grid.utils.net
|
|
import org.apache.http.HttpEntity
|
import org.apache.http.client.methods.CloseableHttpResponse
|
import org.apache.http.client.methods.HttpGet
|
import org.apache.http.client.methods.HttpPost
|
import org.apache.http.client.methods.HttpRequestBase
|
import org.apache.http.entity.ContentType
|
import org.apache.http.entity.StringEntity
|
import org.apache.http.impl.client.HttpClients
|
|
/**
|
* @author riku
|
* Date: 2020/10/14
|
*/
|
class HttpMethod(
|
private val host: String, private val port: Int, private val isHttps: Boolean = false,
|
) {
|
|
data class MyResponse(
|
val success: Boolean,
|
val m: CloseableHttpResponse,
|
)
|
|
object Head {
|
val TEXT_PLAIN = Pair("Content-Type", "text/plain")
|
}
|
|
private val httpClient = HttpClients.createDefault()
|
private val baseUrl = "${if (isHttps) "https" else "http"}://${host}:${port}"
|
|
init {
|
// Protocol.registerProtocol("https", Protocol("https", SkipCertificateValidation.MySecureProtocolSocketFactory(), port))
|
// if (isHttps) {
|
// httpClient.
|
// httpClient.hostConfiguration.setHost(host, port, Protocol.getProtocol("https"))
|
// } else {
|
// httpClient.hostConfiguration.setHost(host, port)
|
// }
|
}
|
|
fun get(url: String, params: List<Pair<String, String>> = emptyList(), headers: List<Pair<String, String>> = emptyList()): MyResponse {
|
var p: String = ""
|
params.forEach {
|
p += if (p.isBlank()) {
|
it.first + "=" + it.second
|
} else {
|
"&" + it.first + "=" + it.second
|
}
|
}
|
val getMethod = HttpGet("$baseUrl$url?$p")
|
defaultConfig(getMethod)
|
headers.forEach {
|
getMethod.addHeader(it.first, it.second)
|
}
|
|
val res = httpClient.execute(getMethod)
|
return when (res.statusLine.statusCode) {
|
200 -> MyResponse(true, res)
|
else -> MyResponse(false, res)
|
}
|
}
|
|
fun post(url: String, data: String = "", headers: List<Pair<String, String>> = emptyList()): MyResponse {
|
val postMethod = HttpPost(baseUrl + url)
|
defaultConfig(postMethod)
|
headers.forEach {
|
postMethod.setHeader(it.first, it.second)
|
}
|
if (data.isNotBlank()) {
|
postMethod.entity = StringEntity(data, ContentType.APPLICATION_JSON)
|
}
|
|
val res = httpClient.execute(postMethod)
|
return when (res.statusLine.statusCode) {
|
200 -> MyResponse(true, res)
|
else -> MyResponse(false, res)
|
}
|
}
|
|
fun post(url: String, entity: HttpEntity, headers: List<Pair<String, String>> = emptyList()):
|
MyResponse {
|
val postMethod = HttpPost(baseUrl + url)
|
headers.forEach {
|
postMethod.addHeader(it.first, it.second)
|
}
|
postMethod.entity = entity
|
|
val res = httpClient.execute(postMethod)
|
return when (res.statusLine.statusCode) {
|
200 -> MyResponse(true, res)
|
else -> MyResponse(false, res)
|
}
|
}
|
|
private fun defaultConfig(method: HttpRequestBase) {
|
method.addHeader("Accept", "*/*");
|
method.addHeader("Connection", "Keep-Alive");
|
// method.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
|
method.addHeader("Accept-Encoding", "gzip,deflate,br");
|
// method.setRequestHeader("Content-Type", "application/json;charset=utf-8")
|
method.addHeader("Content-Type", "application/json")
|
// method.setRequestHeader("Content-Type", "application/json;charset=GBK")
|
}
|
|
}
|