mirror of
https://github.com/tursom/TursomServer.git
synced 2025-04-01 15:11:08 +08:00
fox http request bugs
This commit is contained in:
parent
4a4ceb3dd3
commit
4464bee33b
@ -1,10 +1,13 @@
|
|||||||
package cn.tursom.core
|
package cn.tursom.core
|
||||||
|
|
||||||
import java.io.*
|
import java.io.File
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.io.OutputStream
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLEncoder
|
import java.net.URLEncoder
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import java.util.zip.GZIPInputStream
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向指定URL发送GET方法的请求
|
* 向指定URL发送GET方法的请求
|
||||||
@ -17,144 +20,150 @@ import java.nio.charset.Charset
|
|||||||
* @deprecated
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
fun sendGet(
|
fun sendGet(
|
||||||
url: String,
|
url: String,
|
||||||
param: String? = null,
|
param: String? = null,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): String {
|
): String {
|
||||||
val realUrl = URL(if (param != null) {
|
val realUrl = URL(if (param != null) {
|
||||||
"$url?$param"
|
"$url?$param"
|
||||||
} else {
|
} else {
|
||||||
url
|
url
|
||||||
})
|
})
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val connection = realUrl.openConnection()
|
val connection = realUrl.openConnection()
|
||||||
// 设置请求属性
|
// 设置请求属性
|
||||||
headers.forEach {
|
headers.forEach {
|
||||||
connection.addRequestProperty(it.key, it.value)
|
connection.addRequestProperty(it.key, it.value)
|
||||||
}
|
}
|
||||||
// 建立实际的连接
|
// 建立实际的连接
|
||||||
connection.connect()
|
connection.connect()
|
||||||
// 获取所有响应头字段
|
// 获取所有响应头字段
|
||||||
//val map = connection.headerFields
|
//val map = connection.headerFields
|
||||||
// 遍历所有的响应头字段
|
// 遍历所有的响应头字段
|
||||||
// for (key in map.keys) {
|
// for (key in map.keys) {
|
||||||
// println(key + "--->" + map[key])
|
// println(key + "--->" + map[key])
|
||||||
// }
|
// }
|
||||||
// 定义 BufferedReader输入流来读取URL的响应
|
// 定义 BufferedReader输入流来读取URL的响应
|
||||||
return InputStreamReader(connection.getInputStream(), Charset.defaultCharset()).readText()
|
return connection.getInputStream().let {
|
||||||
|
if (connection.contentEncoding?.contains("gzip") == true) {
|
||||||
|
GZIPInputStream(it)
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}.readBytes().toUTF8String()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendGet(
|
fun sendGet(
|
||||||
url: String,
|
url: String,
|
||||||
param: Map<String, String>,
|
param: Map<String, String>,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): String {
|
): String {
|
||||||
val paramSB = StringBuilder()
|
val paramSB = StringBuilder()
|
||||||
return sendGet(url, run {
|
return sendGet(url, run {
|
||||||
param.forEach {
|
param.forEach {
|
||||||
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
||||||
}
|
}
|
||||||
paramSB.toString()
|
paramSB.toString()
|
||||||
}, headers)
|
}, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendGet(
|
fun sendGet(
|
||||||
url: String,
|
url: String,
|
||||||
outputStream: OutputStream,
|
outputStream: OutputStream,
|
||||||
param: String? = null,
|
param: String? = null,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) {
|
) {
|
||||||
val realUrl = URL(if (param != null) {
|
val realUrl = URL(if (param != null) {
|
||||||
"$url?$param"
|
"$url?$param"
|
||||||
} else {
|
} else {
|
||||||
url
|
url
|
||||||
})
|
})
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val connection = realUrl.openConnection()
|
val connection = realUrl.openConnection()
|
||||||
// 设置请求属性
|
// 设置请求属性
|
||||||
headers.forEach {
|
headers.forEach {
|
||||||
connection.addRequestProperty(it.key, it.value)
|
connection.addRequestProperty(it.key, it.value)
|
||||||
}
|
}
|
||||||
// 建立实际的连接
|
// 建立实际的连接
|
||||||
connection.connect()
|
connection.connect()
|
||||||
|
|
||||||
// 读取URL的响应
|
// 读取URL的响应
|
||||||
connection.getInputStream().use {
|
connection.getInputStream().use {
|
||||||
it.copyTo(outputStream)
|
it.copyTo(outputStream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendGet(
|
fun sendGet(
|
||||||
url: String,
|
url: String,
|
||||||
outputStream: OutputStream,
|
outputStream: OutputStream,
|
||||||
param: Map<String, String>,
|
param: Map<String, String>,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) {
|
) {
|
||||||
val paramSB = StringBuilder()
|
val paramSB = StringBuilder()
|
||||||
sendGet(url, outputStream, run {
|
sendGet(url, outputStream, run {
|
||||||
param.forEach {
|
param.forEach {
|
||||||
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
||||||
}
|
}
|
||||||
paramSB.toString()
|
paramSB.toString()
|
||||||
}, headers)
|
}, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFile(url: String, filename: String) {
|
fun getFile(url: String, filename: String) {
|
||||||
File(filename).outputStream().use {
|
File(filename).outputStream().use {
|
||||||
sendGet(url, it)
|
sendGet(url, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendHead(
|
fun sendHead(
|
||||||
url: String,
|
url: String,
|
||||||
param: String,
|
param: String,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): Map<String, List<String>> {
|
): Map<String, List<String>> {
|
||||||
val urlNameString = "$url?$param"
|
val urlNameString = "$url?$param"
|
||||||
val realUrl = URL(urlNameString)
|
val realUrl = URL(urlNameString)
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val connection = realUrl.openConnection() as HttpURLConnection
|
val connection = realUrl.openConnection() as HttpURLConnection
|
||||||
// 设置请求属性
|
// 设置请求属性
|
||||||
headers.forEach {
|
headers.forEach {
|
||||||
connection.addRequestProperty(it.key, it.value)
|
connection.addRequestProperty(it.key, it.value)
|
||||||
}
|
}
|
||||||
connection.requestMethod = "HEAD"
|
connection.requestMethod = "HEAD"
|
||||||
// 建立实际的连接
|
// 建立实际的连接
|
||||||
connection.connect()
|
connection.connect()
|
||||||
// 获取响应头字段
|
// 获取响应头字段
|
||||||
return connection.headerFields
|
return connection.headerFields
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendHead(
|
fun sendHead(
|
||||||
url: String,
|
url: String,
|
||||||
param: Map<String, String>,
|
param: Map<String, String>,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): Map<String, List<String>> {
|
): Map<String, List<String>> {
|
||||||
val paramSB = StringBuilder()
|
val paramSB = StringBuilder()
|
||||||
return sendHead(url, run {
|
return sendHead(url, run {
|
||||||
param.forEach {
|
param.forEach {
|
||||||
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
paramSB.append("${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}&")
|
||||||
}
|
}
|
||||||
paramSB.toString()
|
paramSB.toString()
|
||||||
}, headers)
|
}, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -169,189 +178,189 @@ fun sendHead(
|
|||||||
*/
|
*/
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun sendPost(
|
fun sendPost(
|
||||||
url: String,
|
url: String,
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): String {
|
): String {
|
||||||
val realUrl = URL(url)
|
val realUrl = URL(url)
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val conn = realUrl.openConnection()
|
val conn = realUrl.openConnection()
|
||||||
// 设置通用的请求属性
|
// 设置通用的请求属性
|
||||||
headers.forEach { key, value ->
|
headers.forEach { key, value ->
|
||||||
conn.setRequestProperty(key, value)
|
conn.setRequestProperty(key, value)
|
||||||
}
|
}
|
||||||
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
||||||
conn.doOutput = true
|
conn.doOutput = true
|
||||||
conn.doInput = true
|
conn.doInput = true
|
||||||
|
|
||||||
// 获取URLConnection对象对应的输出流
|
// 获取URLConnection对象对应的输出流
|
||||||
conn.outputStream.use { out ->
|
conn.outputStream.use { out ->
|
||||||
// 发送请求参数
|
// 发送请求参数
|
||||||
out.write(data)
|
out.write(data)
|
||||||
// flush输出流的缓冲
|
// flush输出流的缓冲
|
||||||
out.flush()
|
out.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定义BufferedReader输入流来读取URL的响应
|
// 定义BufferedReader输入流来读取URL的响应
|
||||||
return InputStreamReader(conn.getInputStream(), Charset.defaultCharset()).readText()
|
return conn.getInputStream().readBytes().toUTF8String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun sendPost(
|
fun sendPost(
|
||||||
url: String,
|
url: String,
|
||||||
param: Map<String, String>,
|
param: Map<String, String>,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
): String {
|
): String {
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
param.forEach { key, value ->
|
param.forEach { key, value ->
|
||||||
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
||||||
}
|
}
|
||||||
sb.deleteCharAt(sb.length - 1)
|
sb.deleteCharAt(sb.length - 1)
|
||||||
return sendPost(url, sb.toString().toByteArray(), headers)
|
return sendPost(url, sb.toString().toByteArray(), headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun sendPost(
|
fun sendPost(
|
||||||
url: String,
|
url: String,
|
||||||
outputStream: OutputStream,
|
outputStream: OutputStream,
|
||||||
param: ByteArray,
|
param: ByteArray,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) {
|
) {
|
||||||
val realUrl = URL(url)
|
val realUrl = URL(url)
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val conn = realUrl.openConnection()
|
val conn = realUrl.openConnection()
|
||||||
// 设置通用的请求属性
|
// 设置通用的请求属性
|
||||||
headers.forEach { (key, value) ->
|
headers.forEach { (key, value) ->
|
||||||
conn.setRequestProperty(key, value)
|
conn.setRequestProperty(key, value)
|
||||||
}
|
}
|
||||||
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
||||||
conn.doOutput = true
|
conn.doOutput = true
|
||||||
conn.doInput = true
|
conn.doInput = true
|
||||||
|
|
||||||
// 获取URLConnection对象对应的输出流
|
// 获取URLConnection对象对应的输出流
|
||||||
conn.outputStream.use { out ->
|
conn.outputStream.use { out ->
|
||||||
// 发送请求参数
|
// 发送请求参数
|
||||||
out.write(param)
|
out.write(param)
|
||||||
// flush输出流的缓冲
|
// flush输出流的缓冲
|
||||||
out.flush()
|
out.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取URL的响应
|
// 读取URL的响应
|
||||||
conn.getInputStream().copyTo(outputStream)
|
conn.getInputStream().copyTo(outputStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun sendPost(
|
fun sendPost(
|
||||||
url: String,
|
url: String,
|
||||||
outputStream: OutputStream,
|
outputStream: OutputStream,
|
||||||
param: Map<String, String>,
|
param: Map<String, String>,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) {
|
) {
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
param.forEach { (key, value) ->
|
param.forEach { (key, value) ->
|
||||||
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
||||||
}
|
}
|
||||||
sb.deleteCharAt(sb.length - 1)
|
sb.deleteCharAt(sb.length - 1)
|
||||||
sendPost(url, outputStream, sb.toString().toByteArray(), headers)
|
sendPost(url, outputStream, sb.toString().toByteArray(), headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("MemberVisibilityCanBePrivate")
|
@Suppress("MemberVisibilityCanBePrivate")
|
||||||
class HttpRequest(
|
class HttpRequest(
|
||||||
val url: String,
|
val url: String,
|
||||||
val param: ByteArray? = null,
|
val param: ByteArray? = null,
|
||||||
val headers: Map<String, String> = mapOf(
|
val headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) {
|
) {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
url: String,
|
url: String,
|
||||||
param: Map<String, String>? = null,
|
param: Map<String, String>? = null,
|
||||||
headers: Map<String, String> = mapOf(
|
headers: Map<String, String> = mapOf(
|
||||||
Pair("accept", "*/*"),
|
Pair("accept", "*/*"),
|
||||||
Pair("connection", "Keep-Alive"),
|
Pair("connection", "Keep-Alive"),
|
||||||
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
Pair("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"))
|
||||||
) : this(
|
) : this(
|
||||||
url,
|
url,
|
||||||
param?.run {
|
param?.run {
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
param.forEach { (key, value) ->
|
param.forEach { (key, value) ->
|
||||||
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
sb.append("${URLEncoder.encode(key, "utf-8")}=${URLEncoder.encode(value, "utf-8")}&")
|
||||||
}
|
}
|
||||||
sb.deleteCharAt(sb.length - 1)
|
sb.deleteCharAt(sb.length - 1)
|
||||||
sb.toString().toByteArray()
|
sb.toString().toByteArray()
|
||||||
},
|
},
|
||||||
headers)
|
headers)
|
||||||
|
|
||||||
|
|
||||||
var body: String? = null
|
var body: String? = null
|
||||||
var head: Map<String, List<String>>? = null
|
var head: Map<String, List<String>>? = null
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun get() {
|
fun get() {
|
||||||
body = null
|
body = null
|
||||||
head = null
|
head = null
|
||||||
val realUrl = URL(if (param != null) {
|
val realUrl = URL(if (param != null) {
|
||||||
"$url?${String(param)}"
|
"$url?${String(param)}"
|
||||||
} else {
|
} else {
|
||||||
url
|
url
|
||||||
})
|
})
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val connection = realUrl.openConnection() as HttpURLConnection
|
val connection = realUrl.openConnection() as HttpURLConnection
|
||||||
// 设置请求属性
|
// 设置请求属性
|
||||||
this.headers.forEach {
|
this.headers.forEach {
|
||||||
connection.addRequestProperty(it.key, it.value)
|
connection.addRequestProperty(it.key, it.value)
|
||||||
}
|
}
|
||||||
// 建立实际的连接
|
// 建立实际的连接
|
||||||
connection.connect()
|
connection.connect()
|
||||||
// 获取响应头字段
|
// 获取响应头字段
|
||||||
head = connection.headerFields
|
head = connection.headerFields
|
||||||
body = InputStreamReader(connection.inputStream).readText()
|
body = InputStreamReader(connection.inputStream).readText()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun post() {
|
fun post() {
|
||||||
body = null
|
body = null
|
||||||
head = null
|
head = null
|
||||||
val realUrl = URL(url)
|
val realUrl = URL(url)
|
||||||
// 打开和URL之间的连接
|
// 打开和URL之间的连接
|
||||||
val connection = realUrl.openConnection() as HttpURLConnection
|
val connection = realUrl.openConnection() as HttpURLConnection
|
||||||
|
|
||||||
// 设置请求属性
|
// 设置请求属性
|
||||||
this.headers.forEach {
|
this.headers.forEach {
|
||||||
connection.addRequestProperty(it.key, it.value)
|
connection.addRequestProperty(it.key, it.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
// 发送POST请求必须设置如下两行,HttpUrlConnection会将请求方法自动设置为POST
|
||||||
connection.doOutput = true
|
connection.doOutput = true
|
||||||
connection.doInput = true
|
connection.doInput = true
|
||||||
|
|
||||||
// 建立实际的连接
|
// 建立实际的连接
|
||||||
connection.connect()
|
connection.connect()
|
||||||
|
|
||||||
// 获取URLConnection对象对应的输出流
|
// 获取URLConnection对象对应的输出流
|
||||||
if (param != null) connection.outputStream.use { out ->
|
if (param != null) connection.outputStream.use { out ->
|
||||||
// 发送请求参数
|
// 发送请求参数
|
||||||
out.write(param)
|
out.write(param)
|
||||||
// flush输出流的缓冲
|
// flush输出流的缓冲
|
||||||
out.flush()
|
out.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取响应头字段
|
// 获取响应头字段
|
||||||
head = connection.headerFields
|
head = connection.headerFields
|
||||||
body = InputStreamReader(connection.inputStream).readText()
|
body = InputStreamReader(connection.inputStream).readText()
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user