在 .net 4.5 中使用 ClientWebSocket 时如何设置 User-Agent 和 Referer 标头? [英] How can I set User-Agent and Referer headers when using ClientWebSocket in .net 4.5?

查看:38
本文介绍了在 .net 4.5 中使用 ClientWebSocket 时如何设置 User-Agent 和 Referer 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ClientWebSocket.SetHeader 的明显答案会引发异常,因为它是受保护的标头:

<前>发生 System.ArgumentExceptionMessage=必须使用适当的属性或方法修改User-Agent"标头.参数名称:名称参数名称=名称堆栈跟踪:在 System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)

异常字符串建议在 ClientWebSocket 本身上使用属性/方法,但我找不到任何这样的属性/方法.这个异常似乎是为 HttpWebRequest 类设计的,它实际上具有这样的属性.

无效的代码:

ClientWebSocket socket = new ClientWebSocket();//会抛出socket.Options.SetRequestHeader("User-Agent", "SomeUserAgentString");//会抛出socket.Options.SetRequestHeader("Referer", "SomeReferer"]);

解决方案

您似乎无法设置这些属性,至少现在不能.你可能可以通过反射来做到这一点.

如果您仔细查看堆栈跟踪,您会发现抛出方法是 System.Net.WebHeaderCollection.ThrowOnRestrictedHeader.System.Net.WebHeaderCollection 是专门用于处理 HTTP 标头的名称值集合.如果您查看备注部分,您会看到以下内容:

<块引用>

一些常见的标头被认为是受限制的,要么直接由 API 公开(例如 Content-Type),要么受系统保护且无法更改.

该列表将 User-AgentReferer 属性列为受保护的标头,并且无法设置,因为 ClientWebSocket 未公开它.

尽管如此,如果您绝对需要设置这些标头,则需要找到您的 ClientWebSocketOptions<的私有引用 WebHeaderCollection/code>(作为 ClientWebSocket 上的 Options 属性公开)并调用受保护的 AddWithoutValidate 方法来设置标题.

The obvious answer of using ClientWebSocket.SetHeader throws an exception because it's a protected header:

System.ArgumentException occurred
  Message=The 'User-Agent' header must be modified using the appropriate property or method.
Parameter name: name
  ParamName=name
  StackTrace:
       at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)

The exception string suggests using a property/method on the ClientWebSocket itself but I can't find any such property/method. It seems this exception was designed for the HttpWebRequest class, which actually has such property.

The code, which doesn't work:

ClientWebSocket socket = new ClientWebSocket();
// Will throw
socket.Options.SetRequestHeader("User-Agent", "SomeUserAgentString");
// Will throw
socket.Options.SetRequestHeader("Referer", "SomeReferer"]);

解决方案

It doesn't look like you'll be able to set those properties, at least not right now. You might be able to do it via reflection.

If you look closely at your stack trace, you'll see that the throwing method is System.Net.WebHeaderCollection.ThrowOnRestrictedHeader. System.Net.WebHeaderCollection is a specialized name value collection designed to deal with HTTP headers. If you look at the remarks section, you'll see the following:

Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed.

The list has both the User-Agent and Referer properties listed as protected headers and cannot be set since the ClientWebSocket does not expose it.

All that being said, though, if you absolutely need to set those headers, you'll need to find the private reference WebHeaderCollection of your ClientWebSocketOptions (exposed as the Options property on your ClientWebSocket) and call the protected AddWithoutValidate method to set the headers.

这篇关于在 .net 4.5 中使用 ClientWebSocket 时如何设置 User-Agent 和 Referer 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆