【问题标题】:Call to .NET Rest Web Service throws Bad Request调用 .NET Rest Web 服务引发错误请求
【发布时间】:2014-02-06 13:16:20
【问题描述】:

我在调用我的 .NET 应用程序提供的与字符编码相关的 REST Web 服务时遇到问题。

我正在发送这样的内容:

http://:/DotNetApplication/RestService.svc/eyJudW1iZXIiOiI5NDU5NjA5M%2BDAiLCJtc2ciOiJQYXJhIGNvbmZpcm1hciBhIHJlY

查询字符串是这个,它是 URL 编码的,由于 %2B (+) 字符,我收到 Bad Request - Invalid URL 错误。

如果未指定此字符,Web 服务将成功响应。

我做错了什么?

谢谢

【问题讨论】:

  • 如果你对 % 字符进行 url 编码呢?
  • 什么意思?如果我对 % 字符进行编码,我会得到类似 %252B 的内容,对吗?

标签: .net rest


【解决方案1】:

请尝试对% 字符进行编码,如果它自己生成%

根据RFC 2396, page 9

Because the percent "%" character always has the reserved purpose of
being the escape indicator, it must be escaped as "%25" in order to
be used as data within a URI.  Implementers should be careful not to
escape or unescape the same string more than once, since unescaping
an already unescaped string might lead to misinterpreting a percent
data character as another escaped character, or vice versa in the
case of escaping an already escaped string.

【讨论】:

  • 将其更改为 http://:/DotNetApplication/RestService.svc/eyJudW1iZXIiOiI5NDU5NjA5M%252BDAiLCJtc2ciOiJQYXJhIGNvbmZpcm1hciBhIHJlY 问题仍然存在
【解决方案2】:

这似乎不是 %2b 的问题,而是字符大小的问题。我正在使用 get,但 URL 的长度约为 350 个字符。

【讨论】:

    【解决方案3】:

    问题不在于 %2B 字符,而在于解析数据的格式。

    不正确: http://:/DotNetApplication/RestService.svc/eyJudW1iZXIiOiI5NDU5NjA5M%2BDAiLCJtc2ciOiJQYXJhIGNvbmZpcm1hciBhIHJlY

    正确(查询字符串) http://:/DotNetApplication/RestService.svc/?request=eyJudW1iZXIiOiI5NDU5NjA5M%2BDAiLCJtc2ciOiJQYXJhIGNvbmZpcm1hciBhIHJlY

    由于数据是通过路径结构而不是标准查询字符串传递给 REST Web 服务的,因此字符数限制为 254。

    谢谢

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多