为什么在获取lyricsify.com时,HttpClient.GetStringAsync的响应为空?

huangapple go评论65阅读模式
英文:

Why is HttpClient.GetStringAsync response empty when fetching lyricsify.com?

问题

I want to use GetStringAsync to query lyricsify.com but I always get an empty string as result.

With Insomnia, Powershell and other tools, I get the HTML response as expected.

[Fact]
public async void HttpClientTest()
{
    HttpClient httpClient = new HttpClient();
    string googleResponse = await httpClient.GetStringAsync("https://www.google.com");
    Debug.WriteLine($"googleResponse: {googleResponse}");
    // works as expected
    Assert.NotEmpty(googleResponse);

    string lyricsifyResponse = await httpClient.GetStringAsync("https://www.lyricsify.com");
    Debug.WriteLine($"lyricsifyResponse: {lyricsifyResponse}");
    // fails
    Assert.NotEmpty(lyricsifyResponse);
}
  • I checked the response object, it has status code 200 OK.
  • I checked redirects, which are enabled.

Any ideas?
Did I miss something peculiar that lyricsify.com is doing?
Do I need to configure anything in the HttpClient to get the expected web page as response?

英文:

I want to use GetStringAsync to query lyricsify.com but I always get an empty string as result.

With Insomnia, Powershell and other tools, I get the HTML response as expected.

[Fact]
public async void HttpClientTest()
{
    HttpClient httpClient = new HttpClient();
    string googleResponse = await httpClient.GetStringAsync("https://www.google.com");
    Debug.WriteLine($"googleResponse: {googleResponse}");
    // works as expected
    Assert.NotEmpty(googleResponse);

    string lyricsifyResponse = await httpClient.GetStringAsync("https://www.lyricsify.com");
    Debug.WriteLine($"lyricsifyResponse: {lyricsifyResponse}");
    // fails
    Assert.NotEmpty(lyricsifyResponse);
}
  • I checked the response object, it has status code 200 OK.
  • I checked redirects, which are enabled.

Any ideas?
Did I miss something peculiar that lyricsify.com is doing?
Do I need to configure anything in the HttpClient to get the expected web page as response?

答案1

得分: 3

Some websites expected to pass "User-Agent," so only pass some random values as "User-Agent" like this:

HttpClient httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Add("User-Agent", "some-thing");

// rest of code
英文:

Some websites expected to pass User-Agent, so only pass some random values as User-Agent like this:

HttpClient httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Add("User-Agent", "some-thing");

// rest of code

答案2

得分: -1

A header can be added to the request.
As follows;

request.Headers.Add("HeaderName","Value");

You can also test this with Postman.

英文:

A header can be added to the request.
As follows;

request.Headers.Add("HeaderName","Value");

You can also test this with Postman.

huangapple
  • 本文由 发表于 2023年5月25日 22:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定