Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpHeaders user-agent not working #851

Closed
faner11 opened this issue Jul 4, 2020 · 2 comments
Closed

HttpHeaders user-agent not working #851

faner11 opened this issue Jul 4, 2020 · 2 comments

Comments

@faner11
Copy link

faner11 commented Jul 4, 2020

HttpHeaders user-agent not working

flutter 1.17.4
Dio 3.0.9
Repro rate 100%

一个非常诡异的问题,当我使用dio请求时request.headers 显示ua已经设置,但是并没有生效。

final ua =
        "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";

final url = "https://v.douyin.com/JLcscxJ/";

void dioHttp(String url, String ua) async {
  try {
    final response = await Dio().get(url,
          options: Options(headers: {HttpHeaders.userAgentHeader: ua}));
      print(response.data.toString().length);
    } catch (e) {
      print(e);
    }
}

void ioHttp(String url, String ua) async {
    final uri = Uri.parse(url);
    final httpClient = new HttpClient();
    httpClient.userAgent = ua;
    final request = await httpClient.getUrl(uri);
    final response = await request.close();
    final responseBody = await response.transform(Utf8Decoder()).join();
    print(responseBody.length);
}

经过一些尝试,我可以通过ioHttp这个方法获得正确的返回,dioHttp这个方法无法得到正常的返回,让我诡异的是两个http请求的headers是相同的。

@faner11
Copy link
Author

faner11 commented Jul 5, 2020

我找到新的可行的方法,但是我希望client.userAgent = null 成为默认的配置

  void dioHttp(String url, String ua) async {
    var dio = Dio();
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (HttpClient client) {
      client.userAgent = null;
    };
    var req = await dio.get(url,
        options: Options(headers: {HttpHeaders.userAgentHeader: ua}));
    print(req.data.toString().length);
  }

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Aug 4, 2020
@wendux wendux closed this as completed in 516825f Aug 7, 2020
wendux added a commit that referenced this issue Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant