google / cronet-transport-for-okhttp

This package allows OkHttp and Retrofit users to use Cronet as their transport layer, benefiting from features like QUIC/HTTP3 support or connection migration.
Apache License 2.0
427 stars 31 forks source link

How to modify http request headers? #3

Closed bcfhe closed 2 years ago

bcfhe commented 2 years ago

Is it possible to modify the http request headers?

Danstahrg commented 2 years ago

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.

bcfhe commented 2 years ago

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))
    }
}
Danstahrg commented 2 years ago

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

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.

bcfhe commented 2 years ago

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?

ag2s20150909 commented 2 years ago

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

Danstahrg commented 2 years ago

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

bcfhe commented 2 years ago

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