facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.23k stars 24.33k forks source link

[Android] Inappropriate network user agent #3138

Closed matth3wga0 closed 8 years ago

matth3wga0 commented 9 years ago

Hi,

Currently making network request will contains the default user agent from okhttp, e.g. okhttp/2.4.0, this's not the proper way on the iOS which sets the user agent to product_name/bundle_version.

andreicoman11 commented 9 years ago

On android, the user agent has to be set in the networking module by the app itself. This can be done in MainReactPackage, by providing the default user agent to the module. The default one the system provides is System.getProperty("http.agent"). It is also possible to change the constructor to set the default user agent to the one received by the system, but I remember @mkonicek decided against it. Do you remember why?

mkonicek commented 9 years ago

@andreicoman11 I used the okhttp default for simplicity.

@matth3wga0 I like the idea of sending product_name/bundle_version. Happy to review a pull request for the CLI template where you set the user agent of the NetworkingModule. You'd have to introduce a separate package that overrides the NetworkingModule.

andreicoman11 commented 9 years ago

I would advise to also append the system user agent to the default user agent. The default okhttp user agent doesn't do much.

matth3wga0 commented 9 years ago

Sorry @mkonicek, I'm not an Android developer, but I'd like to help this, could you give me some advises?

mkonicek commented 9 years ago

You can get this working inside a sample app first and then port those changes to the CLI template. You can make changes to the Java code and run 'react-native run-android' to have your app built and installed to the device. Then it's just a matter on hacking on Java code :) This particular change doesn't need much Android knowledge.

matth3wga0 commented 9 years ago

In a nutshell, I tried and failed.

Instead of overriding NetworkingModule, I want to modify MainReactPackage.java, initialize NetworkingModule with the second parameter.

new NetworkingModule(reactContext, defaultUserAgent)

Since I can't access MainReactPackage.java in the sample app which create with react-native init sample, so I tried to modify the examples UIExplorer inside the master repo, followed with this instruction to install it on my device (5.0.2), but it force stopped immediately once I launched it, the packager is up, and JSMainModule looks fine.

http://localhost:8081/Examples/Movies/MoviesApp.android.bundle?platform=android

Neither UIExplorer nor Movies will force close, but the app created with react-native cli works, 😖

carobeta commented 7 years ago

It's possible to set the user-agent for all network request? Using the same for Android and IOS?