WebRequest 从 Uri 中删除 Fragment

发布于 2024-12-01 03:40:51 字数 1278 浏览 1 评论 0原文

我想打开以下页面:

<一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:

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=0CC0QpwUoAA&tbs=cat:895%2Cprice%3A1%2Cppr_min%3A50%2Cppr_max%3A99.99&tbm=shop&tbo=&fp=1&biw=851&bih=426&cad=b&bav=on.2,or.r_gc.r_pw.

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画骨成沙 2024-12-08 03:40:51

虽然示例中的片段并不完全符合片段的设计目的,但它仍然向浏览器提供客户端数据,以便在页面初始加载后采取操作。如果您在单击链接时仔细观察,您会看到页面标题开头为“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.

梦里人 2024-12-08 03:40:51

据我所知,# 代表一个片段(通常是网页中的锚点),实际上并不是 URL 的一部分,它只是将浏览器带到页面的特定部分,并且仅在客户端进行处理。

换句话说,它不是 URI 请求的一部分。

片段标识符的功能与其余部分不同
URI:即,它的处理完全是客户端的,没有
来自服务器的参与 - 当然服务器通常会提供帮助
确定 MIME 类型,MIME 类型确定
片段的处理。

当代理(例如 Web 浏览器)
向 Web 服务器请求资源,代理将 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.

The fragment identifier functions differently than the rest of the
URI: namely, its processing is exclusively client-side with no
participation from the server
— of course the server typically helps
to determine the MIME type, and the MIME type determines the
processing of fragments.

When an agent (such as a Web browser)
requests a resource from a Web server, the agent sends the URI to the
server, but does not send the fragment. Instead, the agent waits for
the server to send the resource, and then the agent processes the
resource according to the document type and fragment value.

http://www.w3.org/TR/webarch/#media-type-fragid

情仇皆在手 2024-12-08 03:40:51

这是一个奇怪的现象,因为fragment确实不应该由服务器解析。但是,在您的示例网址中,该片段肯定正在使用(也许在谷歌JS,客户端?)

这是一个快速技巧,它似乎可以通过编码整个查询来完成您需要的操作:

string str =
"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=0CC0QpwUoAA&tbs=cat:895%2Cprice%3A1%2Cppr_min%3A50%2Cppr_max%3A99.99&tbm=shop&tbo=&fp=1&biw=851&bih=426&cad=b&bav=on.2,or.r_gc.r_pw.";

string[] arr = str.Split( '?' );
string uriString = arr[0] + "?" + HttpUtility.UrlEncode( arr[1] );
var uri = new Uri( uriString );
var request = ( HttpWebRequest )WebRequest.Create( uri );

编辑
回想起来,fragment 是用在客户端 JS 上的,而不是服务器端。

更好的想法可能是了解谷歌的 Url,即您的 Uri 应该是:

http://www.google.com/search?q=car+speaker&hl=en&tbs=cat:895,price:1,ppr_min:50&tbm=shop

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:

string str =
"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=0CC0QpwUoAA&tbs=cat:895%2Cprice%3A1%2Cppr_min%3A50%2Cppr_max%3A99.99&tbm=shop&tbo=&fp=1&biw=851&bih=426&cad=b&bav=on.2,or.r_gc.r_pw.";

string[] arr = str.Split( '?' );
string uriString = arr[0] + "?" + HttpUtility.UrlEncode( arr[1] );
var uri = new Uri( uriString );
var request = ( HttpWebRequest )WebRequest.Create( uri );

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:

http://www.google.com/search?q=car+speaker&hl=en&tbs=cat:895,price:1,ppr_min:50&tbm=shop
迟到的我 2024-12-08 03:40:51

片段是客户端机制,因此在服务器上没有用处。您传回服务器的任何数据都将位于查询字符串中,那么尝试传回片段有什么好处呢?

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?

宁愿没拥抱 2024-12-08 03:40:51

如果您在客户端需要此变量,则可以使用此 javascript 代码

 <script>
 if(window.location.hash) {
    console.log(window.location.hash); 
    window.location.hash = window.location.hash;
 } 
</script>

If you need this variable on client side then you can use this javascript code

 <script>
 if(window.location.hash) {
    console.log(window.location.hash); 
    window.location.hash = window.location.hash;
 } 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文