英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论