gpt4 book ai didi

kotlin - 如何在 Webview 和 Retrofit2 Okhttp3 之间共享 cookie

转载 作者:行者123 更新时间:2023-12-02 12:11:35 29 4
gpt4 key购买 nike

我有一个有 2 个请求的站点:

第一个是看到一个装有食材的篮子(无需登录)

GET https://www.ah.nl/mijnlijst2

第二个是在篮子中插入元素的请求。
POST https://www.ah.nl/common/api/basket/v2/add
{
"items": [{
"quantity": 1,
"id": 395948
}]
}

在我的应用程序中,我做了 3 件事:
  • 使用隐藏的 webview 初始化 cookie:
    initializeWebView(binding.webView, "https://www.ah.nl/mijnlijst2")

    var myCookie = ""

    @SuppressLint("JavascriptInterface", "SetJavaScriptEnabled")
    fun initializeWebView(webView: WebView, url: String?) {
    val settings = webView.settings
    settings.javaScriptEnabled = true
    settings.builtInZoomControls = false
    settings.setGeolocationEnabled(true)
    settings.setAppCacheEnabled(false)
    settings.loadsImagesAutomatically = true
    webView.webViewClient = WebViewClient()
    webView.loadUrl(url)

    webView.webViewClient = object : WebViewClient() {

    override fun onPageFinished(webView: WebView?, url: String) {
    super.onPageFinished(webView, url)

    val syncManager =
    CookieSyncManager.createInstance(webView?.context)
    val cookieManager: CookieManager = CookieManager.getInstance()
    cookieManager.setAcceptThirdPartyCookies(webView,true)

    if (cookieManager.getCookie(url)!= null) {
    val cookie: String = cookieManager.getCookie(url)
    myCookie = cookie
    Timber.d("Cookie = $myCookie")
    }

    syncManager.sync()
    }
    }

    webView.loadUrl(url)
    }

  • 2:插入成分:
    val response = api.insertIngredient(WebViewManager.myCookie, ingredients).execute()

    interface AHApi {

    @Headers(
    "content-type: application/json; charset=utf-8",
    "accept-encoding: gzip, deflate, br"
    )
    @POST("common/api/basket/v2/add")
    fun insertIngredient(
    @Header("cookie") cookie: String,
    @Body ingredients: AhIngredientRequest
    ): Call<ResponseBody>
    }

    data class AhIngredientRequest(

    @Expose
    @SerializedName("items")
    val items: List<AhItem>
    )

    data class AhItem(

    @SerializedName("id")
    var id: Long = 0L,

    @SerializedName("quantity")
    var quantity: Long = 1L
    )

    @Provides
    @Singleton
    fun provideAHApi(context: Context): AHApi{
    val interceptor = HttpLoggingInterceptor()
    if (BuildConfig.DEBUG) {
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
    } else {
    interceptor.setLevel(HttpLoggingInterceptor.Level.NONE)
    }

    val timeout = 30L
    val client: OkHttpClient = OkHttpClient.Builder()
    .readTimeout(timeout, TimeUnit.SECONDS)
    .writeTimeout(timeout, TimeUnit.SECONDS)
    .connectTimeout(timeout, TimeUnit.SECONDS)
    .followRedirects(false)
    .build()

    val retrofit: Retrofit = Retrofit.Builder()
    .baseUrl(context.getString(R.string.ah_api))
    .addConverterFactory(MoshiConverterFactory.create().asLenient())
    .client(client)
    .build()
    return retrofit.create(AHApi::class.java)
    }
  • 在 webview 中启动项目列表:
    class WebviewFragment : Fragment() {

    private lateinit var binding: FragmentWebviewBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView = inflater.inflate(R.layout.fragment_webview, container, false)
    binding = DataBindingUtil.bind(rootView)!!

    WebViewManager.initializeWebView(binding.webView, "https://www.ah.nl/mijnlijst2")

    return rootView
    }
    }
  • OkHttp3 响应返回 200 OK。
  • WebView 保持为空,0 项。我希望我插入的项目会在那里,因为我与 setAcceptThirdPartyCookies 共享了我的 cookie .
  • 但:
    当我第一次手动添加成分时,
    然后点击购物 list
    然后回到我的应用程序,
    并再次加载 webview,
    从这一刻起,一切正常。

  • 因此,似乎通过手动初始化 cookie
  • 发布 https://www.ah.nl/common/api/basket/v2/add
  • https://www.ah.nl/mijnlijst2

  • 有效,但是当我以编程方式执行时,它不会。

    最佳答案

    您还需要设置 cookieManager到改造 HTTP 客户端 (OkHttp)。

    您可以使用 JavaNetCookieJar 来自 okhttp-urlconnection 包,一个包装类,委托(delegate)给 java.net.CookieHandler :

    implementation "com.squareup.okhttp3:okhttp-urlconnection:4.6.0"

    val httpClient = OkHttpClient.Builder()
    .cookieJar(JavaNetCookieJar(cookieManager))
    .build()
    val retrofit = Retrofit.Builder()
    .client(httpClient)
    .build()

    关于kotlin - 如何在 Webview 和 Retrofit2 Okhttp3 之间共享 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61572689/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com