WebRequest 从 Uri 中删除 Fragment
我想打开以下页面:
<一href="http://www.google.com/search?q=dvd+player&tbm=shop&hl=en&aq=f#q=car+speaker+&hl=en&sa=X&ei =vbtSTqTPDKXniAKy0-iDAw&ved=0CC0Qp wUoAA&tbs=cat:895%2Cprice%3A1%2Cppr_min%3A50%2Cppr_max%3A99.99&tbm=商店&tbo=&fp=1&biw=851&bih=426&cad=b&bav=on.2,or.r_gc.r_pw" rel="nofollow">http://www.google.com/search?q=dvd+player&tbm=shop&hl=en&aq=f#q=car+speaker+&hl=en&sa= X&ei=vbtSTqTPDKXniAKy0-iDAw&ved=0CC0Q pwUoAA&tbs=cat:895%2Cprice%3A1%2Cppr_min%3A50%2Cppr_max%3A99.99&tbm=shop&am p;tbo=&fp=1&biw=851&bih=426&cad=b&bav=on.2,or.r_gc.r_pw。
但是当我尝试使用 WebRequest
类打开它时,它会从 url 中删除“Fragment”(在本例中不是 Uri 片段),并尝试打开以下 url:
http://www. google.com/search?q=dvd+player&tbm=shop&hl=en&aq=f
我该如何打开该网址?
我尝试对 #
字符进行编码,但这似乎不起作用。
编辑:
我知道 URL 是什么,片段是什么,等等。我要问的是是否有一种方法可以覆盖 Uri
类的功能。 (获取 Uri,并删除 #
之后的内容并将其放置在 Fragment
属性中,等等),这样我就可以查询该 url,而不是修改后的 url。
I want to open the following page:
but when I try to open it using WebRequest
class, it removes the "Fragment" (which is not a Uri Fragment in this case) from the url, and tries to open the following url instead:
http://www.google.com/search?q=dvd+player&tbm=shop&hl=en&aq=f
How can I do to open exactly that url?
I tried encoding the #
character, but that does not seem to be working.
Edit:
I know what the URL is, what the fragment is, and all of it. What I'm asking is if there is a way to override what the Uri
class does. (To take the Uri, and remove what is after #
and place it in the Fragment
property, etc), so I can query that url, instead of the modified url.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然示例中的片段并不完全符合片段的设计目的,但它仍然向浏览器提供客户端数据,以便在页面初始加载后采取操作。如果您在单击链接时仔细观察,您会看到页面标题开头为“dvd 播放器”(来自原始查询字符串),但在页面从服务器加载后更改为“汽车扬声器”,并且客户端脚本执行。
While the fragment in your example doesn't do exactly what a fragment was designed to do, it's still providing client-side data to the browser to take action after the page has been initially loaded. If you watch carefully when clicking on the link, you'll see the title of the page starts out as "dvd player" (from the original query string) but then changes to "car speaker" after the page has loaded from the server and the client-side script executes.
据我所知,# 代表一个片段(通常是网页中的锚点),实际上并不是 URL 的一部分,它只是将浏览器带到页面的特定部分,并且仅在客户端进行处理。
换句话说,它不是 URI 请求的一部分。
http://www.w3.org/TR/webarch/#media-类型脆弱
As far as I know the # represents a fragment (usually an anchor in web pages) and isn't really part of the URL, it just takes the browser to a specific part of the page, and is process on the client-side only.
In other words, it's not part of the URI request.
http://www.w3.org/TR/webarch/#media-type-fragid
这是一个奇怪的现象,因为
fragment
确实不应该由服务器解析。但是,在您的示例网址中,该片段肯定正在使用(也许在谷歌JS,客户端?)这是一个快速技巧,它似乎可以通过编码整个查询来完成您需要的操作:
编辑
回想起来,
fragment
是用在客户端 JS 上的,而不是服务器端。更好的想法可能是了解谷歌的 Url,即您的 Uri 应该是:
This is a strange one in that the
fragment
really shouldn't be parsed by the server. But, in your example url, the fragment most definitely is being used (maybe in the google JS, client side?)Here's a quick hack that seems to do what you need by encoding the entire query:
Edit
In retrospect, the
fragment
is being used on the client side JS, not server side.A better idea may be to understand google's Url's, i.e., your Uri should be:
片段是客户端机制,因此在服务器上没有用处。您传回服务器的任何数据都将位于查询字符串中,那么尝试传回片段有什么好处呢?
The fragment is a client-side mechanism, and thus has no use on the server. Any data you pass back to the server would be in the querystring, so what benefit do you see of attempting to pass a fragment back?
如果您在客户端需要此变量,则可以使用此 javascript 代码
If you need this variable on client side then you can use this javascript code