简体   繁体   English

HttpClient.GetAsync() 选择性地忽略查询参数

[英]HttpClient.GetAsync() selectively ignoring query parameter

I have a very simple method for querying a webpage and parsing the results.我有一个非常简单的方法来查询网页并解析结果。 The body of the response isn't the problem, the problem is that the request generated by the GetAsync(string) or GetAsync(Uri) methods seem to selectively ignore a certain query parameter.响应的正文不是问题,问题在于GetAsync(string)GetAsync(Uri)方法生成的请求似乎选择性地忽略了某个查询参数。 I say it is selectively ignoring it, because it is ignoring the same parameter regardless of the order in which they appear.我说它是选择性地忽略它,因为无论它们出现的顺序如何,它都会忽略相同的参数。

When I check the RequestMessage property of the returned HttpResponseMessage , the RequestUri shows the complete uri correctly, except that it's missing the very specific "o=data" parameter, and the response body confirms that the parameter wasn't present in the request because the content should be sorted by date if it was present (but it isn't).当我检查返回的HttpResponseMessageRequestMessage属性时, RequestUri正确显示了完整的 uri,只是它缺少非常具体的“o=data”参数,并且响应正文确认请求中不存在该参数,因为如果内容存在(但不存在),则应按日期对内容进行排序。

static readonly HttpClient client = new() { BaseAddress = new("https://www.jusbrasil.com.br/diarios/busca") };

public static async Task<ResultDocument[]> GetResults(string name)
        {
            var query = new Uri($"https://www.jusbrasil.com.br/diarios/busca?q={Uri.EscapeDataString(name)}&o=data");
            //var query = "?q={name}&o=data" the exact same problem happens whether I use BaseAddress or not.
            var resp = await client.GetAsync(query);
            // Rest of the function parsing the result's body.
        }

具体来说,“o”查询参数从请求中消失了。

UPDATE: I've tried to rename the parameter.更新:我试图重命名参数。 It works if the parameter is named "w", and it works if the parameter is named "or". 如果参数命名为“w”,它会起作用,如果参数命名为“or”,它就会起作用。 It ONLY disappears if it has the name "o" (which is the one I need). 只有当它具有名称“o”(这是我需要的)时它才会消失。 Doesn't work.不工作。 Any other parameter disappears if name has a space character.如果name包含空格字符,则任何其他参数都会消失。

UPDATE: Turns out the problem only happens when name has a space character.更新:原来问题只发生在name有空格字符时。 Even if it escaped.就算逃了。 Code updated.代码已更新。

No space:没有空间: 没有空间

With space:有空间: 有空间

Showing name.ToCharArray() when "o" disappears.当“o”消失时显示name.ToCharArray() (The method here is being called directly from Program.cs): (这里的方法是直接从 Program.cs 调用的): 在此处输入图像描述

UPDATE: It seems the problem might be with the default HttpMessageHandler .更新:似乎问题可能出在默认的HttpMessageHandler When I implement a custom handler just to see how HttpClient is generating the HttpRequestMessage , the RequestUri is correct in the request.当我实现自定义处理程序只是为了查看HttpClient如何生成HttpRequestMessage时,请求中的RequestUri是正确的。 And also notice that here the space character shows escaped as "%20" and not as '+' , which is how it should be, since Uri.EscapeDataString(string) escapes it with "%20" and not '+' .还要注意,这里的空格字符显示为"%20"而不是'+'转义,这是应该的,因为Uri.EscapeDataString(string)"%20"而不是'+'转义它。 在此处输入图像描述

With a lot of help from @Dai, we finally figured out the problem.在@Dai 的大力帮助下,我们终于找到了问题所在。 The server responded with a redirect if the space character in the name was escaped with %20 , and the redirection destination ignored the other parameters.如果name中的空格字符用%20转义,则服务器以重定向响应,并且重定向目标忽略其他参数。 It turns out the problem was with the server, and not the code.事实证明问题出在服务器上,而不是代码上。

To fix this I simply changed the line:为了解决这个问题,我只是改变了这一行:

var query = new Uri($"https://www.jusbrasil.com.br/diarios/busca?q={Uri.EscapeDataString(name)}&o=data");

To:至:

var query = new Uri($"https://www.jusbrasil.com.br/diarios/busca?q={Uri.EscapeDataString(name).Replace("%20", "+")}&o=data");

I left the Uri.EscapeDataString(string) and only replaced %20 because other characters escaped with %xx (such as double quote %22 ) still works fine and causes no faulty redirection.我离开了Uri.EscapeDataString(string)并且只替换了%20因为用%xx转义的其他字符(例如双引号%22 )仍然可以正常工作并且不会导致重定向错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM