zoukankan      html  css  js  c++  java
  • C# WebRequest.Create 锚点“#”字符问题

    背景

    在与后台API接口对接时,如将网页Url作为参数请求数据时,如果是锚点参数,则会丢失。

    锚点参数

    请求通过WebRequest.Create创建一个WebRequest:

    1 var uri = "https://id.test.xxx.com/api/v1/auth/sso/url?redirectUrl=https://test.xxx.com/extend/userhelp#120";
    2 var webRequest = WebRequest.Create(uri);

    得到的webRequest,发现后面的锚点参数没有了。如下图:

    获取的webRequest,Address和RequestUri中的Query参数,不包含锚点参数“#120”

    正常的查询参数

    我们换正常的查询参数,再试验如上步骤

    通过WebRequest.Create创建一个WebRequest:

    1 var uri = "https://id.test.xxx.com/api/v1/auth/sso/url?redirectUrl=https://test.xxx.com/extend/userhelp?id=120";
    2 var webRequest = WebRequest.Create(uri);

    得到的webRequest,查询Address和RequestUri中的Query参数。如下图:

    后面的查询参数"?id=120",数据正常。

    总结

    原因:WebRequest.Create方法中,锚点参数会在另一属性中出现,

    如果有锚点参数,并调用WebRequest.Create,临时解决方案:

    1. 在调用WebRequest.Create时Uri不添加锚点参数,调用完后再自行拼接锚点参数
    2. 从WebRequest.RequestUri.Fragment中获取锚点参数,重新组合成Uri.

    值得注意的是,生成Request后,请求服务器API接口,默认后台是不会处理锚点信息的。所以如果需要支持锚点,需要API接口支持。

    关键字:WebRequest.Create 锚点#参数丢失

  • 相关阅读:
    react-echarts之折线图的显示
    Log4j2
    测试驱动开发Junit4
    JavaWeb基础: Cookie
    JavaWeb前端:Bootstrap基础
    JavaWeb前端:CSS
    JavaWeb前端:JQuery
    Android基础:Activity
    JavaWeb前端: JavaScript 简介
    JavaWeb前端:HTML5 简介
  • 原文地址:https://www.cnblogs.com/kybs0/p/9962003.html
Copyright © 2011-2022 走看看