Closed bcfhe closed 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.
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))
}
}
Ah, right, there's a few things that may come into play here:
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.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.
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?
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
Caveat to that: The only officially supported platform is Android.
On Linux and Windows, it seems difficult to use this library. So I will use this only on android. Thank you for your advice.
Is it possible to modify the http request headers?