【问题标题】:WCF REST Going over Max URL Length causes errorWCF REST 超过最大 URL 长度会导致错误
【发布时间】:2011-07-27 13:14:57
【问题描述】:

我正在尝试使用 WCF Rest 创建日志服务。它看起来像这样:

[ServiceContract]
public interface ILoggingService
{
    [OperationContract, WebGet(UriTemplate = "/LogError?m={message}")]
    void Log(string message);
}

我增加了配置文件中的限制,以便您可以记录大量文本。但是,如果我超过此限制,服务将不接受该消息。到目前为止,我已经确保文本低于限制,但这是一个糟糕的解决方法。如何在 WCF REST 中解决此问题。

更新

经过进一步调查,我最终应该得到这样的东西?

[ServiceContract]
public interface ILoggingService
{
    [OperationContract, WebGet(UriTemplate = "/LogError?m={message}", Method = "POST")]
    void Log(string message);
}

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/plain";

【问题讨论】:

标签: c# .net wcf rest wcf-rest


【解决方案1】:

我建议不要使用 GET 方法。您正在执行的操作是写入,而不是读取。如果使用 POST,则可以使用媒体类型 text/plain 发送实体正文。这样你就不受 url 长度的限制了。

【讨论】:

  • 那么当方法是 POST 时我将如何执行 HttpWebRequest?
  • 添加了解释何时使用每种 http 请求类型的 wiki 链接。
【解决方案2】:

对于 http 帖子,我认为签名看起来像

    [OperationContract]
    [WebInvoke(UriTemplate = "LogError",Method="POST")]
    void Log(string message);

使用帖子在 http 请求中而不是在 url 中发送数据。

http get请求只能用于读取数据,post创建,put更新,delete删除。

http://en.wikipedia.org/wiki/Representational_State_Transfer

【讨论】:

  • 在技术上(和设计上)可能的情况下,仅使用 GET 的想法是否存在安全问题?
猜你喜欢
  • 2017-04-08
  • 2019-09-22
  • 2021-06-30
  • 2019-10-10
  • 2012-10-08
  • 1970-01-01
  • 2018-01-14
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多