Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter fetch a webpage with user-agent parameter

I am using flutter http package for fetch a webpage. I want to send http request like desktop.How can I do that ?

My User-Agent : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"

my code ;

  String text="mert";
  var isLoading = false;



 _fetchData() async {


final response =
    await http.get("http://trscript.net");
if (response.statusCode == 200) {

 var document = parse(response.body.toString());
 if(document.querySelector("body > div > div.sol.yaklas > div:nth-child(1) > article > div.yazi_bilgi")!=null){
    text = document.querySelector("body > div > div.sol.yaklas > div:nth-child(1) > article > div.yazi_bilgi").text;
 }

} else {
  throw Exception('Failed to load photos');
}

}

like image 524
bymerdo Avatar asked Oct 30 '25 20:10

bymerdo


1 Answers

Based on http package latest version documentation you should extend http.BaseClient. A code snipped directly copied from docs:

lass UserAgentClient extends http.BaseClient {
  final String userAgent;
  final http.Client _inner;

  UserAgentClient(this.userAgent, this._inner);

  Future<http.StreamedResponse> send(http.BaseRequest request) {
    request.headers['user-agent'] = userAgent;
    return _inner.send(request);
  }
}
like image 102
mamadshr Avatar answered Nov 01 '25 14:11

mamadshr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!