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 锚点#参数丢失

  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/kybs0/p/9962003.html
Copyright © 2011-2022 走看看