Skip to content

Commit

Permalink
Merge pull request #69 from sergemat/master
Browse files Browse the repository at this point in the history
#68 Fixed issue with missing and outdated HTTP Header
  • Loading branch information
sergemat authored Mar 6, 2024
2 parents ec60515 + 49dc7c7 commit cb999f4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions YahooFinanceApi/Yahoo - Historical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Task<Stream> _GetResponseStreamAsync(CancellationToken token)

return url
.WithCookie(YahooSession.Cookie.Name, YahooSession.Cookie.Value)
.WithHeader(YahooSession.UserAgentKey, YahooSession.UserAgentValue)
.GetAsync(token)
.ReceiveStream();
}
Expand Down
1 change: 1 addition & 0 deletions YahooFinanceApi/Yahoo - Quote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public async Task<IReadOnlyDictionary<string, Security>> QueryAsync(Cancellation
{
data = await url
.WithCookie(YahooSession.Cookie.Name, YahooSession.Cookie.Value)
.WithHeader(YahooSession.UserAgentKey, YahooSession.UserAgentValue)
.GetAsync(token)
.ReceiveJson()
.ConfigureAwait(false);
Expand Down
5 changes: 3 additions & 2 deletions YahooFinanceApi/YahooFinanceApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<Description>A handy Yahoo! Finance api wrapper, based on .NET standard 2.0</Description>
<Copyright>Copyright (c) 2016-2024 Karl Wan</Copyright>
<AssemblyTitle>YahooFinanceApi</AssemblyTitle>
<VersionPrefix>2.3.2</VersionPrefix>
<VersionPrefix>2.3.3</VersionPrefix>
<LangVersion>latest</LangVersion>
<Authors>Karl Wan</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>YahooFinanceApi</AssemblyName>
<PackageId>YahooFinanceApi</PackageId>
<PackageTags>Yahoo;Finance;Stock;Quote;Eod;Dividend;Split</PackageTags>
<PackageReleaseNotes>
[03/06/2024] Fixed issue with missing HTTP Header
[02/22/2024] Fixed issue with HTTP status code 500 when creating Yahoo session
[12/10/2023] Fixed issue with HTTP status code 502 when creating Yahoo session
[5/8/2023] Fixed too many requests error caused by missing user agent header
Expand Down Expand Up @@ -41,7 +42,7 @@
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<Version>2.3.2</Version>
<Version>2.3.3</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
Expand Down
17 changes: 12 additions & 5 deletions YahooFinanceApi/YahooSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ internal static class YahooSession
private static FlurlCookie _cookie;
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);

/// <summary>
/// The user agent key for HTTP Header
/// </summary>
public const string UserAgentKey = "User-Agent";

/// <summary>
/// The user agent value for HTTP Header
/// </summary>
public const string UserAgentValue = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0";

/// <summary>
/// Gets or sets the auth crumb.
/// </summary>
Expand Down Expand Up @@ -58,14 +68,11 @@ public static async Task InitAsync(CancellationToken token = default)
await _semaphore.WaitAsync(token).ConfigureAwait(false);
try
{
const string userAgentKey = "User-Agent";
const string userAgentValue = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";

var response = await "https://fc.yahoo.com"
.AllowHttpStatus("404")
.AllowHttpStatus("500")
.AllowHttpStatus("502")
.WithHeader(userAgentKey, userAgentValue)
.WithHeader(UserAgentKey, UserAgentValue)
.GetAsync()
.ConfigureAwait(false);

Expand All @@ -81,7 +88,7 @@ public static async Task InitAsync(CancellationToken token = default)

_crumb = await "https://query1.finance.yahoo.com/v1/test/getcrumb"
.WithCookie(_cookie.Name, _cookie.Value)
.WithHeader(userAgentKey, userAgentValue)
.WithHeader(UserAgentKey, UserAgentValue)
.GetAsync(token)
.ReceiveString();

Expand Down

0 comments on commit cb999f4

Please sign in to comment.