Skip to content

Commit 49dc7c7

Browse files
committed
karlwancl#68 Fixed issue with missing and outdated HTTP Header
1 parent ec60515 commit 49dc7c7

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

YahooFinanceApi/Yahoo - Historical.cs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Task<Stream> _GetResponseStreamAsync(CancellationToken token)
119119

120120
return url
121121
.WithCookie(YahooSession.Cookie.Name, YahooSession.Cookie.Value)
122+
.WithHeader(YahooSession.UserAgentKey, YahooSession.UserAgentValue)
122123
.GetAsync(token)
123124
.ReceiveStream();
124125
}

YahooFinanceApi/Yahoo - Quote.cs

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public async Task<IReadOnlyDictionary<string, Security>> QueryAsync(Cancellation
9292
{
9393
data = await url
9494
.WithCookie(YahooSession.Cookie.Name, YahooSession.Cookie.Value)
95+
.WithHeader(YahooSession.UserAgentKey, YahooSession.UserAgentValue)
9596
.GetAsync(token)
9697
.ReceiveJson()
9798
.ConfigureAwait(false);

YahooFinanceApi/YahooFinanceApi.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
<Description>A handy Yahoo! Finance api wrapper, based on .NET standard 2.0</Description>
55
<Copyright>Copyright (c) 2016-2024 Karl Wan</Copyright>
66
<AssemblyTitle>YahooFinanceApi</AssemblyTitle>
7-
<VersionPrefix>2.3.2</VersionPrefix>
7+
<VersionPrefix>2.3.3</VersionPrefix>
88
<LangVersion>latest</LangVersion>
99
<Authors>Karl Wan</Authors>
1010
<TargetFramework>netstandard2.0</TargetFramework>
1111
<AssemblyName>YahooFinanceApi</AssemblyName>
1212
<PackageId>YahooFinanceApi</PackageId>
1313
<PackageTags>Yahoo;Finance;Stock;Quote;Eod;Dividend;Split</PackageTags>
1414
<PackageReleaseNotes>
15+
[03/06/2024] Fixed issue with missing HTTP Header
1516
[02/22/2024] Fixed issue with HTTP status code 500 when creating Yahoo session
1617
[12/10/2023] Fixed issue with HTTP status code 502 when creating Yahoo session
1718
[5/8/2023] Fixed too many requests error caused by missing user agent header
@@ -41,7 +42,7 @@
4142
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
4243
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
4344
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
44-
<Version>2.3.2</Version>
45+
<Version>2.3.3</Version>
4546
</PropertyGroup>
4647
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
4748
<LangVersion>latest</LangVersion>

YahooFinanceApi/YahooSession.cs

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ internal static class YahooSession
1515
private static FlurlCookie _cookie;
1616
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
1717

18+
/// <summary>
19+
/// The user agent key for HTTP Header
20+
/// </summary>
21+
public const string UserAgentKey = "User-Agent";
22+
23+
/// <summary>
24+
/// The user agent value for HTTP Header
25+
/// </summary>
26+
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";
27+
1828
/// <summary>
1929
/// Gets or sets the auth crumb.
2030
/// </summary>
@@ -58,14 +68,11 @@ public static async Task InitAsync(CancellationToken token = default)
5868
await _semaphore.WaitAsync(token).ConfigureAwait(false);
5969
try
6070
{
61-
const string userAgentKey = "User-Agent";
62-
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";
63-
6471
var response = await "https://fc.yahoo.com"
6572
.AllowHttpStatus("404")
6673
.AllowHttpStatus("500")
6774
.AllowHttpStatus("502")
68-
.WithHeader(userAgentKey, userAgentValue)
75+
.WithHeader(UserAgentKey, UserAgentValue)
6976
.GetAsync()
7077
.ConfigureAwait(false);
7178

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

8289
_crumb = await "https://query1.finance.yahoo.com/v1/test/getcrumb"
8390
.WithCookie(_cookie.Name, _cookie.Value)
84-
.WithHeader(userAgentKey, userAgentValue)
91+
.WithHeader(UserAgentKey, UserAgentValue)
8592
.GetAsync(token)
8693
.ReceiveString();
8794

0 commit comments

Comments
 (0)