티스토리 뷰
728x90
Example
private fun token(email: String, password: String): String {
val url = "https://asdf.com/tokens"
val headers = mapOf(
"Accept" to "application/json",
"Content-Type" to "application/json",
"Authorization" to "null"
)
val json = mapOf(
"auth_type" to "CREDENTIAL",
"credential_type_payload" to mapOf(
"email" to email,
"password" to password,
),
).toJson()
return CrawlUtil.post(url, headers, json)["token"] as String
}
CrawlUtil
import org.jsoup.Connection
import org.jsoup.Jsoup
object CrawlUtil {
private const val TIMEOUT = 5 * 60 * 1000
private const val MAX_BODY_SIZE = 0
private const val IGNORE_CONTENT_TYPE = true
private fun connection(
url: String,
headers: Map<String, String>,
json: String,
data: Map<String, String>? = emptyMap()
): Connection {
var connection = Jsoup.connect(url)
headers.forEach {
connection = connection.header(it.key, it.value)
}
data!!.forEach {
connection = connection.data(it.key, it.value)
}
return connection
.timeout(TIMEOUT)
.maxBodySize(MAX_BODY_SIZE)
.ignoreContentType(IGNORE_CONTENT_TYPE)
.requestBody(json)
}
fun get(
url: String,
headers: Map<String, String>,
json: String,
data: Map<String, String>? = emptyMap()
): Map<String, Any> {
return connection(url, headers, json, data)
.get()
.text()
.toObject()
}
fun post(
url: String,
headers: Map<String, String>,
json: String,
data: Map<String, String>? = emptyMap()
): Map<String, Any> {
return connection(url, headers, json, data)
.post()
.text()
.toObject()
}
}
ExtensionUtil
inline fun <reified T> String.toObject(): T {
return JacksonUtil.toObject(this)
}
JacksonUtil
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
import com.fasterxml.jackson.module.afterburner.AfterburnerModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object JacksonUtil {
val objectMapper: ObjectMapper = ObjectMapper()
.registerModule(KotlinModule.Builder().build())
.registerModule(JavaTimeModule())
.registerModule(AfterburnerModule())
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.registerModule(
JavaTimeModule().apply {
addDeserializer(LocalDate::class.java, LocalDateDeserializer(DateTimeFormatter.ISO_DATE))
addDeserializer(
LocalDateTime::class.java,
LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)
)
}
)
fun toJson(obj: Any?): String {
if (obj == null) {
return ""
}
return objectMapper.writeValueAsString(obj)
}
fun merged(a: MutableMap<String, Any>, b: Any): Map<String, Any> {
return objectMapper
.readerForUpdating(a)
.readValue(toJson(b))
}
inline fun <reified T> toObject(s: String): T {
return objectMapper.readValue(s, object : TypeReference<T>() {})
}
}
https://stackoverflow.com/questions/27463036/jsoup-http-post-with-payload
https://partnerjun.tistory.com/43
https://stackoverflow.com/questions/10012399/how-to-post-data-into-website-using-jsoup
https://yj-something.tistory.com/2
728x90
'공부' 카테고리의 다른 글
댓글
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 책그림
- 테슬라 리퍼럴 코드 혜택
- Bot
- 테슬라
- 테슬라 레퍼럴
- 연애학개론
- 유투브
- 모델 Y 레퍼럴
- 모델y
- 클루지
- 테슬라 추천
- COUNT
- 테슬라 레퍼럴 코드 확인
- follower
- 테슬라 리퍼럴 코드 생성
- 할인
- 김달
- Kluge
- 팔로워 수 세기
- 인스타그램
- 레퍼럴
- 개리마커스
- 테슬라 리퍼럴 코드
- wlw
- 어떻게 능력을 보여줄 것인가?
- 테슬라 레퍼럴 적용 확인
- 테슬라 크레딧 사용
- 메디파크 내과 전문의 의학박사 김영수
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함