0

I'm trying to add web platform support to one of my flutter apps.

This application needs to imitate the user-agent of the mobile device to get the information from the web page and works fine on windows:

Future<String> _getLiveHtml(String url) async {
  var resp = await http.get(
    Uri.parse(url),
    headers: {
      'User-Agent':
          'Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
    },
  );
  return resp.body;
}

But when I run the app in Chrome, I find that the request will be redirected because Chrome is not using my custom user-agent but Chrome's own user-agent:

GET /kpl HTTP/1.1
Accept: */*
Connection: keep-alive
Host: m.huya.com
Referer: http://localhost:4573/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36
sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"

If I modify the simulated device model to any mobile device in Chrome's developer tools, the request will not be redirected, it seems that custom user-agent in flutter web does not work,what should I do?

Jamfly
  • 30
  • 4

0 Answers0