如何从httpresponsemessage获取串行的URL?

发布于 2025-02-10 12:18:58 字数 1303 浏览 2 评论 0 原文

有没有办法从响应中获取完全序列化的URL地址?

给定下面的请求,我想获取以下字符串: http://192.168.0.72:8080/manufacturer 如果可能的话。实际上,这是发送到服务器的URL - 但这不是我可以提取的。

此示例的范围有限,因为它没有证明查询参数的用法 ,但是使用 baseaddress 的用法确实意味着URI添加到 getAsync < EM>并不总是是发送到服务器的实际URL。许多事情可以在它的过程中操纵它。

例如,如果我要将查询参数传递 {“ search”:“ test”,“ locale”:“ en-us”} 中,我希望能获得 https://192.168.0.72:8080/manufacturer?search=test&amp;locale = en-us

public static HttpClient InitializeHttpClient() {
    var client = new HttpClient();

    client.BaseAddress = new Uri("http://192.168.0.72:8080/");
    client.Timeout = TimeSpan.FromSeconds(3);

    client.DefaultRequestHeaders.Add(
        "User-Agent", "My App Name"
    );

    return client;
}
private static readonly HttpClient Client = Helpers.InitializeHttpClient();
HttpResponseMessage response = await Client.GetAsync("manufacturer");
Stream content = await response.Content.ReadAsStreamAsync();
string url = response?.RequestMessage.RequestUri.ToString(); // "manufacturer"

作为参考,我是在dotnet 6.0上运行Xamarin形式的,建立在Android上。

Is there a way to acquire the fully serialized URL address from a response?

Given the request below, I'd like to acquire the following string: http://192.168.0.72:8080/manufacturer if possible. This in fact is the URL that is sent to the server - but it's not what I'm able to extract.

This example is limited in scope as it does not demonstrate usage of query parameters, but usage of BaseAddress does mean that the Uri fed into GetAsync will not always be the actual URL sent to the server. Many things can manipulate it on it's way.

For example, if I was to pass in the query parameters {"search": "test", "locale": "en-US"} into the request, I'd hope to get https://192.168.0.72:8080/manufacturer?search=test&locale=en-US.

public static HttpClient InitializeHttpClient() {
    var client = new HttpClient();

    client.BaseAddress = new Uri("http://192.168.0.72:8080/");
    client.Timeout = TimeSpan.FromSeconds(3);

    client.DefaultRequestHeaders.Add(
        "User-Agent", "My App Name"
    );

    return client;
}
private static readonly HttpClient Client = Helpers.InitializeHttpClient();
HttpResponseMessage response = await Client.GetAsync("manufacturer");
Stream content = await response.Content.ReadAsStreamAsync();
string url = response?.RequestMessage.RequestUri.ToString(); // "manufacturer"

For reference, I am on DotNet 6.0 running Xamarin Forms, building on Android.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文