Git Product home page Git Product logo

Comments (7)

Danstahrg avatar Danstahrg commented on May 3, 2024

Hi,

For most headers, you can use the OkHttp APIs to modify the request headers. There's a few headers with special treatment, such as accept-encoding and user-agent, if you're seeing an inconsistent behavior between plain OkHttp and this library please provide more details.

from cronet-transport-for-okhttp.

bcfhe avatar bcfhe commented on May 3, 2024

With code like below okhttpclient communicates via HTTP/2 even though the google site supports quic.


import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.net.cronet.okhttptransport.CronetInterceptor
import okhttp3.*
import org.chromium.net.CronetEngine
import java.io.IOException


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val engine = CronetEngine.Builder(applicationContext).build()
        val callFactory = OkHttpClient().newBuilder()
            .addInterceptor(CronetInterceptor.newBuilder(engine).build())
            .build()
        val request=Request.Builder()
            .url("https://www.google.com/")
            .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0")
            .header("Accept-Encoding","gzip, deflate, br")
            .header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
            .build()
        callFactory.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
            }

            override fun onResponse(call: Call, response: Response) {
                runOnUiThread{
                    var contentEncoding=response.headers.get("Content-Encoding")
                    if(contentEncoding.isNullOrEmpty()){
                        contentEncoding="identity"
                    }
                    Toast.makeText(applicationContext,response.protocol.toString()+"\n"+contentEncoding+"\nEncoding:"+response.toString(),
                        Toast.LENGTH_SHORT).show()
                    copyToClipboard(applicationContext,"",response.body!!.string())
                }
            }
        })
    }
    fun copyToClipboard(context: Context, label: String?, text: String?) {
        val clipboardManager: ClipboardManager =
            context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                ?: return
        clipboardManager.setPrimaryClip(ClipData.newPlainText(label, text))
    }
}

from cronet-transport-for-okhttp.

Danstahrg avatar Danstahrg commented on May 3, 2024

Ah, right, there's a few things that may come into play here:

  • In order to use QUIC you need to enable it on your Cronet Engine.
  • You should provide QUIC hints to let Cronet know that a particular host supports QUIC.
  • If you don't we'll pick that information up from alt-svc headers and will use quic the next time you connect to the host. However, note that due to connection pooling the reconnect might not happen immediately so subsequent request to the same host might still use h2.
  • Either way, we also suggest that you enable on-disk cache to persist "these servers support QUIC" signals.

Unrelated to QUIC support - you shouldn't set Accept-Encoding manually. Cronet sets these automatically depending on you Cronet engine configuration (e.g. enableBrotli) and decodes the server response transparently.

from cronet-transport-for-okhttp.

bcfhe avatar bcfhe commented on May 3, 2024

Enabling QUIC and setting QUIC Hints solved this problem.

I also want to use QUIC on non-Android OS such as Linux. Can this library be used on Linux and Windows?

from cronet-transport-for-okhttp.

ag2s20150909 avatar ag2s20150909 commented on May 3, 2024

Enabling QUIC and setting QUIC Hints solved this problem.

I also want to use QUIC on non-Android OS such as Linux. Can this library be used on Linux and Windows?

https://github.com/chromium/chromium/blob/main/components/cronet/build_instructions.md

from cronet-transport-for-okhttp.

Danstahrg avatar Danstahrg commented on May 3, 2024

Caveat to that: The only officially supported platform is Android.

from cronet-transport-for-okhttp.

bcfhe avatar bcfhe commented on May 3, 2024

On Linux and Windows, it seems difficult to use this library. So I will use this only on android.
Thank you for your advice.

from cronet-transport-for-okhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.