| | |
| | | package com.flightfeather.uav.common.net |
| | | |
| | | import org.apache.commons.httpclient.HttpClient |
| | | import org.apache.commons.httpclient.HttpMethodBase |
| | | import org.apache.commons.httpclient.methods.GetMethod |
| | | import org.apache.commons.httpclient.methods.PostMethod |
| | | import org.apache.commons.httpclient.methods.StringRequestEntity |
| | | import org.apache.commons.httpclient.protocol.Protocol |
| | | import org.slf4j.LoggerFactory |
| | | 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 |
| | | private val host: String, private val port: Int, private val isHttps: Boolean = false, |
| | | ) { |
| | | private val logger = LoggerFactory.getLogger(HttpMethod::class.java) |
| | | |
| | | data class MyResponse( |
| | | val success: Boolean, |
| | | val m: HttpMethodBase |
| | | val success: Boolean, |
| | | val m: CloseableHttpResponse, |
| | | ) |
| | | |
| | | object Head { |
| | | val TEXT_PLAIN = Pair("Content-Type", "text/plain") |
| | | } |
| | | |
| | | private val httpClient = HttpClient() |
| | | |
| | | init { |
| | | Protocol.registerProtocol("https", Protocol("https", SkipCertificateValidation.MySecureProtocolSocketFactory(), port)) |
| | | if (isHttps) { |
| | | httpClient.hostConfiguration.setHost(host, port, Protocol.getProtocol("https")) |
| | | } else { |
| | | httpClient.hostConfiguration.setHost(host, port) |
| | | } |
| | | } |
| | | private val httpClient = HttpClients.createDefault() |
| | | private val baseUrl = "${if (isHttps) "https" else "http"}://${host}:${port}" |
| | | |
| | | fun get(url: String, params: List<Pair<String, String>> = emptyList(), headers: List<Pair<String, String>> = emptyList()): MyResponse { |
| | | var p: String = "" |
| | |
| | | "&" + it.first + "=" + it.second |
| | | } |
| | | } |
| | | val getMethod = GetMethod("$url?$p") |
| | | val getMethod = HttpGet("$baseUrl$url?$p") |
| | | defaultConfig(getMethod) |
| | | headers.forEach { |
| | | getMethod.setRequestHeader(it.first, it.second) |
| | | getMethod.addHeader(it.first, it.second) |
| | | } |
| | | |
| | | return when (httpClient.executeMethod(getMethod)) { |
| | | 200 -> MyResponse(true, getMethod) |
| | | else -> MyResponse(false, getMethod) |
| | | 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 = PostMethod(url) |
| | | val postMethod = HttpPost(baseUrl + url) |
| | | defaultConfig(postMethod) |
| | | headers.forEach { |
| | | postMethod.setRequestHeader(it.first, it.second) |
| | | postMethod.setHeader(it.first, it.second) |
| | | } |
| | | if (data.isNotBlank()) { |
| | | postMethod.requestEntity = StringRequestEntity(data, "application/json", "utf-8") |
| | | postMethod.entity = StringEntity(data, ContentType.APPLICATION_JSON) |
| | | } |
| | | |
| | | return try { |
| | | when (httpClient.executeMethod(postMethod)) { |
| | | 200 -> MyResponse(true, postMethod) |
| | | else -> MyResponse(false, postMethod) |
| | | } |
| | | } catch (e: Exception) { |
| | | logger.error(e.message) |
| | | MyResponse(false, postMethod) |
| | | val res = httpClient.execute(postMethod) |
| | | return when (res.statusLine.statusCode) { |
| | | 200 -> MyResponse(true, res) |
| | | else -> MyResponse(false, res) |
| | | } |
| | | } |
| | | |
| | | private fun defaultConfig(method: HttpMethodBase) { |
| | | method.setRequestHeader("Accept", "*/*"); |
| | | method.setRequestHeader("Connection", "Keep-Alive"); |
| | | 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.setRequestHeader("Accept-Encoding", "gzip,deflate,br"); |
| | | method.addHeader("Accept-Encoding", "gzip,deflate,br"); |
| | | // method.setRequestHeader("Content-Type", "application/json;charset=utf-8") |
| | | method.setRequestHeader("Content-Type", "application/json") |
| | | method.addHeader("Content-Type", "application/json") |
| | | // method.setRequestHeader("Content-Type", "application/json;charset=GBK") |
| | | } |
| | | |