localazy / kotlin-mpp-wininet

This project demonstrates how to use the WinInet API in your Kotlin MPP app for simple HTTP communication.
4 stars 0 forks source link

kotlin-mpp-wininet - Cookie Handle #1

Open carlosrafp opened 3 years ago

carlosrafp commented 3 years ago

how to get the cookie returned by a Http get?

vaclavhodek commented 3 years ago

You have to use HttpQueryInfoW with HTTP_QUERY_COOKIE to get the corresponding header.

There's no built-in solution for handling cookies out-of-the-box. The same applies for setting cookies. You need to send Set-Cookie header manually.

carlosrafp commented 3 years ago

Ty, HttpQueryInfoW worked fine for me,

justed added this code in "WinInetHelper.request", after getResponseData() call

sample: var lpszData = ByteArray(200) val dwSize = alloc() val index = alloc() index.value = 0.dword() dwSize.value = 200.dword() lpszData.usePinned { if (HttpQueryInfoW(requestHandle.handle,HTTP_QUERY_SET_COOKIE,it.addressOf(0),dwSize.ptr,index.ptr) == 0){ if (GetLastError().toInt() == ERROR_INSUFFICIENT_BUFFER){ println("ERROR_INSUFFICIENT_BUFFER, required_size = " + dwSize.value) lpszData = ByteArray(dwSize.value.toInt()) index.value = 0.dword() HttpQueryInfoW(requestHandle.handle,HTTP_QUERY_SET_COOKIE,it.addressOf(0),dwSize.ptr,index.ptr) } else if (GetLastError().toInt() == ERROR_HTTP_HEADER_NOT_FOUND){ index.value = 0.dword() HttpQueryInfoW(requestHandle.handle,HTTP_QUERY_COOKIE,it.addressOf(0),dwSize.ptr,index.ptr) println("ERROR_HTTP_HEADER_NOT_FOUND (HTTP_QUERY_SET_COOKIE) - trying HTTP_QUERY_COOKIE") } } } println("size = " + dwSize.value + ", indice = " + index.value + ", cookie = " + lpszData.decodeToString())

carlosrafp commented 3 years ago

Do you Know to to handle https with certificate? In java u should update sdk cacerts, no idea how to do it in klotin/native

vaclavhodek commented 3 years ago

HTTPS is supported by the WinInet library and handled transparently, so the certificate has to be available in system.

It's controller by INTERNET_FLAG_SECURE flag. Check MemScope.openRequest(...) function in WinInetHelper.kt.