获取 WCF REST 服务的 RAW 请求字符串
Get the RAW request String for WCF REST Service
我创建了一些 WCF REST 服务,它们接受 JSON object 请求并返回 JSON object 响应。
Public Function SaveClientInformation(info as ClientInformation) As Boolean Implements IContactInformation.SaveClientInformation
'Here info object is the de-serialized JSON that was sent
'At this point we also need access to the RAW JSON string that was sent from the Java client
dim rawJSONRequest = ?
'We take the info object and persist it out postgress database
End Function
我需要从 Web 服务方法中捕获原始 JSON 字符串。
目前我可以访问反序列化的 object,但我还需要提交给 Web 服务的 RAW JSON 字符串。
我查看了 WCF 文档并环顾四周,不幸的是,这似乎是不可能的。 WCF 不允许我访问发送到服务器的 RAW 请求(字符串)。
我已经尝试创建一个 WCF 消息拦截器,但是这并不能让我访问 RAW 请求字符串,而且我需要可以从 Web 服务方法访问这个 RAW 字符串。
我需要从 Web 服务方法访问 RAW 字符串以及 de-serialized JSON object。
这在 WCF 中可行吗?
我知道这可以使用较新的 ASP.NET Web API 来实现。但我无法更改我们的应用程序,因为我们所有的 Web 服务都构建在 WCF REST (JSON) 之上,并且它们被许多客户端使用。
RAW 请求是必需的,因为在客户端,请求 RAW 字符串由 Java 散列,并且该散列在 header 中发送。因此,访问此 RAW 请求的主要原因是 re-calculate 哈希以检查消息在传输过程中是否未被更改。
仅供参考 - 我们正在使用 HTTPs,但我们需要保证消息未被篡改,这是已在我们的其他 Java Web 服务中实现的技术,我们需要执行在 .NET WCF REST 中也是如此。
您可以通过阅读Request.InputStream获取请求内容。
这需要在 Web.Config.
中打开 aspNetCompatibilityEnabled="true" 设置
见下文:
Dim requestContent = [String].Empty
Dim requestStream = System.Web.HttpContext.Current.Request.InputStream
Dim originalPosition = requestStream.Position
Using reader = New System.IO.StreamReader(requestStream)
requestContent = reader.ReadToEnd()
End Using
requestStream.Seek(originalPosition, SeekOrigin.Begin)
Return requestContent
我创建了一些 WCF REST 服务,它们接受 JSON object 请求并返回 JSON object 响应。
Public Function SaveClientInformation(info as ClientInformation) As Boolean Implements IContactInformation.SaveClientInformation
'Here info object is the de-serialized JSON that was sent
'At this point we also need access to the RAW JSON string that was sent from the Java client
dim rawJSONRequest = ?
'We take the info object and persist it out postgress database
End Function
我需要从 Web 服务方法中捕获原始 JSON 字符串。 目前我可以访问反序列化的 object,但我还需要提交给 Web 服务的 RAW JSON 字符串。
我查看了 WCF 文档并环顾四周,不幸的是,这似乎是不可能的。 WCF 不允许我访问发送到服务器的 RAW 请求(字符串)。
我已经尝试创建一个 WCF 消息拦截器,但是这并不能让我访问 RAW 请求字符串,而且我需要可以从 Web 服务方法访问这个 RAW 字符串。
我需要从 Web 服务方法访问 RAW 字符串以及 de-serialized JSON object。
这在 WCF 中可行吗?
我知道这可以使用较新的 ASP.NET Web API 来实现。但我无法更改我们的应用程序,因为我们所有的 Web 服务都构建在 WCF REST (JSON) 之上,并且它们被许多客户端使用。
RAW 请求是必需的,因为在客户端,请求 RAW 字符串由 Java 散列,并且该散列在 header 中发送。因此,访问此 RAW 请求的主要原因是 re-calculate 哈希以检查消息在传输过程中是否未被更改。
仅供参考 - 我们正在使用 HTTPs,但我们需要保证消息未被篡改,这是已在我们的其他 Java Web 服务中实现的技术,我们需要执行在 .NET WCF REST 中也是如此。
您可以通过阅读Request.InputStream获取请求内容。 这需要在 Web.Config.
中打开 aspNetCompatibilityEnabled="true" 设置见下文:
Dim requestContent = [String].Empty
Dim requestStream = System.Web.HttpContext.Current.Request.InputStream
Dim originalPosition = requestStream.Position
Using reader = New System.IO.StreamReader(requestStream)
requestContent = reader.ReadToEnd()
End Using
requestStream.Seek(originalPosition, SeekOrigin.Begin)
Return requestContent