Comments (7)
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.
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.
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.
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.
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.
Caveat to that: The only officially supported platform is Android.
from cronet-transport-for-okhttp.
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)
- Redirected URL HOT 2
- adding it as an OkHttp interceptor, breaks okhttp3.Authenticator HOT 3
- java.lang.IllegalStateException: There is no read or rewind or length check in progress CronetUploadDataStream HOT 5
- Could not find VERSION HOT 3
- ByteArrayOutputStream size cant be negative HOT 1
- Cronet with Retrofit is crashing due to file-descriptor leaks HOT 4
- Can you upgrade to Okhttp 4? HOT 1
- Consider removing the Guava dependency
- Add a parallel version using android.net.http.HttpEngine
- Requests are resolved through HTTP2 HOT 6
- Option for symbolicated traces HOT 1
- Occasional exceptions occur when sending network requests
- Q: IPC call when using Play Services Cronet provider? HOT 2
- ANRs when loading Cronet Provider HOT 1
- Contribution instructions HOT 2
- setURLStreamHandlerFactory is incompatible with Firebase HOT 3
- Samples should be a working gradle project HOT 1
- After use this core, on firebase monitor dashboard, network request spend more time ...
- Secure DNS support
- Questions about header HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cronet-transport-for-okhttp.