feiyu02
2024-08-27 9ed0b1847912221197697791d69e01ccae17f5b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.flightfeather.uav.common.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}"
 
    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")
    }
 
}