Hello !
I'm using the nuget package version 2.3.0 and while the library works fine in a normal console application, i cant get the library to work consistently on xamarin android,
I'm using latest xamarin.android, made an android unit test app compiling using API 25 target, and min sdk 16, running on 21 emulator( Genymotion ) using visual studio 2015 on windows 10 x64 .
I made an xamrain.android unit tester to test it after it failed in my application:
```c#
using NUnit.Framework;
using YoutubeExplode;
namespace UnitTestYoutubeExplode
{
[TestFixture]
public class TestsSample
{
public string[] ids = { "PMivT7MJ41M"};
[Test, TestCaseSource("ids")]
public async void TestMethod(string id)
{
using(var client = new YoutubeClient())
{
Assert.True(YoutubeClient.ValidateVideoId(id)); //This passed fine all the time
Assert.True(await client.CheckVideoExistsAsync(id)); //This passed fine all the time
var playlistItemInfo = await client.GetVideoInfoAsync(id);
Assert.IsNotNull(playlistItemInfo.Title);
}
}
}
}
All ids are valid.
Originally the test had more ids, but i couldn't get it to pass even with one.
Here are the exceptions i was getting at `await client.GetVideoInfoAsync(id)` :
TestMethod("h--P8HzYZ74") [FAIL] : System.Exception : Could not get video info
at YoutubeExplode.YoutubeClient+
TestMethod("papuvlVeZg8") [FAIL] : System.Exception : Could not get video context
at YoutubeExplode.YoutubeClient+
TestMethod("gL-WVjvzu34") [FAIL] : System.Exception : Could not parse sts
at YoutubeExplode.Internal.Parser.VideoContextFromHtml (System.String rawHtml) [0x00066] in <9d4dbb3394174337b21bfdc5e06fc022>:0
at YoutubeExplode.YoutubeClient+
```
I tried with or without reference to System.Net.Http, different httpclient implementations (Default, Managed,AndroidClientHandler) , debug and release build and nothing make it work consistently.
About 1 in a 10 would work fine, while in the regular console application, i had no problem running 50+ one after another.
The library stated it targets .NET Standard 1.1, which xamarin.android supports, so it should work.
Hello.
First of all, thanks for your issue report.
I'm not sure I understood correctly. The GetVideoInfoAsync method throws in your live Xamarin.Android application but the tests are passing?
The library stated it targets .NET Standard 1.1, which xamarin.android supports, so it should work.
That's correct. If it was incompatible, it wouldn't have compiled.
It looks like the GET requests are failing for some reason.
Can you try the current master branch version and see if the same issue happens?
Worst case scenario, the upstream version has better exceptions so it would be easier to figure out what's going wrong.
Sorry for the confusion,
GetVideoInfoAsync doesn't work on xamarin.android, both my app and the test suit.On the other hand, the same code works fine in a regular c# console application targeting .net4.5.
I will test on the main branch in a bit, and will post results.
I'm not sure if it will help, but googling about issues with HttpClient on Xamarin.Android brought me to a lot of places suggesting to use ModernHttpClient
Using the master branch,here are the new exceptions i got.
TestMethod("PMivT7MJ41M") [FAIL] : System.Exception : Could not parse player version
at YoutubeExplode.Internal.Parser.VideoContextFromHtml (System.String rawHtml) [0x0003c] in D:\Coding projects\C#\Projects\YoutubeExplode\YoutubeExplode\Internal\Parser.cs:124
at YoutubeExplode.YoutubeClient+<GetVideoInfoAsync>d__9.MoveNext () [0x00141] in D:\Coding projects\C#\Projects\YoutubeExplode\YoutubeExplode\YoutubeClient.cs:151
TestMethod("PMivT7MJ41M") [FAIL] : System.Exception : Could not parse sts
at YoutubeExplode.Internal.Parser.VideoContextFromHtml (System.String rawHtml) [0x0006f] in D:\Coding projects\C#\Projects\YoutubeExplode\YoutubeExplode\Internal\Parser.cs:129
at YoutubeExplode.YoutubeClient+<GetVideoInfoAsync>d__9.MoveNext () [0x00141] in D:\Coding projects\C#\Projects\YoutubeExplode\YoutubeExplode\YoutubeClient.cs:151
But i was able to pass the test 50 times successfully one after each other on xamarin.android.
When i removed this line in DefaultRequestService:
_httpClient.DefaultRequestHeaders.Add("Connection", "Close");
I also ran the library own tests (Tests project in the solution), and all 26 passed without this line.
This might do the trick, but i'm sure why is the behavior is different on each platform when this line is present.
Also ModernHttpClient is kinda dead.
Removing Connection: close fixed the issue? That seems really weird. I was thinking maybe Youtube serves a different response when on Android, but that'd require Xamarin to spoof the User-Agent somehow.
Anyway, Connection: close doesn't play an important role anymore, it was a leftover from older code, so it can be safely removed. Just really weird for it to be the culprit.
Also ModernHttpClient is kinda dead.
Didn't know. I just googled and some people were saying native Xamarin HttpClient is kinda buggy.
Might be a good change to remove on the main branch as well ?
Gonna push a new package on friday
Updated