Skip to content

Commit

Permalink
Hỗ trợ multipart form cho http requests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeusmz authored Apr 30, 2024
1 parent cdabc99 commit 04b70ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "net.minevn"
version = "1.0.4"
version = "1.0.5"

apply(plugin = "java")
apply(plugin = "org.jetbrains.kotlin.jvm")
Expand Down
2 changes: 1 addition & 1 deletion minevnlib-bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: MineVNLib
author: MineVN
main: net.minevn.libs.bukkit.MineVNLib
version: 1.0
version: 1.0.5
softdepend: [eco]
32 changes: 24 additions & 8 deletions minevnlib-master/src/main/java/net/minevn/libs/HttpUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.minevn.libs

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.DataOutputStream
import java.io.InputStreamReader
import java.io.*
import java.net.HttpURLConnection
import java.net.Proxy
import java.net.URL
Expand All @@ -19,14 +16,20 @@ fun http(
setCookie: Map<String, String>? = null,
getCookie: MutableMap<String, String>? = null,
proxy: Proxy? = null,
isMultipart: Boolean = false
): String {
val boundary = "===" + System.currentTimeMillis() + "==="
val content = body ?: parameters?.map { it.key + "=" + it.value }?.joinToString("&") { it }
val httpsCon = (URL(url).run { proxy?.let { openConnection(it) } ?: openConnection() } as HttpURLConnection).apply {
doOutput = true
doInput = true
instanceFollowRedirects = false
requestMethod = method
setRequestProperty("Content-Type", contentType)
if (isMultipart) {
setRequestProperty("Content-Type", "multipart/form-data; boundary=$boundary")
} else {
setRequestProperty("Content-Type", contentType)
}
setRequestProperty("charset", "utf-8")
if (content != null) {
setRequestProperty("Content-Length", ByteArrayInputStream(content.toByteArray()).readBytes().size.toString())
Expand All @@ -42,7 +45,18 @@ fun http(
connect()
}

if (content != null) {
if (isMultipart) {
OutputStreamWriter(httpsCon.outputStream).apply {
parameters!!.forEach { (key, value) ->
write("--$boundary\r\n")
write("Content-Disposition: form-data; name=\"$key\"\r\n\r\n")
write("$value\r\n")
}
write("--$boundary--\r\n")
flush()
close()
}
} else if (content != null) {
DataOutputStream(httpsCon.outputStream).apply {
write(ByteArrayInputStream(content.toByteArray()).readBytes())
close()
Expand Down Expand Up @@ -75,7 +89,8 @@ fun get(
setCookie: Map<String, String>? = null,
getCookie: MutableMap<String, String>? = null,
proxy: Proxy? = null,
) = http(url, "GET", contentType, body, parameters, headers, setCookie, getCookie, proxy)
isMultipart: Boolean = false
) = http(url, "GET", contentType, body, parameters, headers, setCookie, getCookie, proxy, isMultipart)

fun post(
url: String,
Expand All @@ -86,4 +101,5 @@ fun post(
setCookie: Map<String, String>? = null,
getCookie: MutableMap<String, String>? = null,
proxy: Proxy? = null,
) = http(url, "POST", contentType, body, parameters, headers, setCookie, getCookie, proxy)
isMultipart: Boolean = false
) = http(url, "POST", contentType, body, parameters, headers, setCookie, getCookie, proxy, isMultipart)

0 comments on commit 04b70ae

Please sign in to comment.