还有另一个“用于访问路径的HTTP动词POST'[我的路径]”是不允许的“

时间:2013-02-07 21:47:03

标签: c# wcf post

我已查看所有相关主题,但没有任何帮助。我不使用url重写,我不点击任何按钮。我的Web服务期望来自其他服务的POST,然后它应该返回200 OK以及某些有效负载。

有些人可能会说“好吧,特定的servlet不支持post”。 最令人困惑的是,当我从Chrome Simple REST客户端发送POST时,它可以工作! 当第三方Web服务向我发送相同的POST时,它会给我一个错误。

这就是我的工作,我使用ASP.NET开发服务器作为Web服务,可以通过隧道从外部到达(感谢这个topic) - >这适用于REST客户端。然后等待POST,这给了我“不允许使用HTTP动词POST来访问路径'EndPoint'”

这是web.config

<httpHandlers>   
      <add verb="*" path="EndPoint"
          type="MyHandler" />     
</httpHandlers> 

我的代码解析POST消息并返回所需的消息:

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var stream = context.Request.InputStream;
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        string xml = Encoding.UTF8.GetString(buffer);

        StringBuilder sb = new StringBuilder();
        XDocument doc = XDocument.Parse(xml);            

        var id = XElement.Parse(xml)
            .Descendants("id")
            .First()
            .Value;

        string file = "c:/message.xml";            
        XDocument d = XDocument.Load(file);

        d.Descendants("context").Where(x => x.Attribute("id").Value == id).Single().SetAttributeValue("value", "54");
        d.Save(file);  

        string responseMessage = @"
            <?xml version=""1.0"" encoding=""UTF-8""?>            
            <Message>                
                <code>Fine</code> 
            </Message>
            ";

        context.Response.StatusCode = 200;     
        context.Response.AddHeader("Content-Type", "application/xml");
        context.Response.Write(responseMessage);            

    }

这是来自隧道monitor

的日志
<!-- 
[HttpException]: The HTTP verb POST used to access path &#39;/EndPoint/&#39; is not allowed.
   at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->

编辑答案很简单。外部服务发送到EndPoint /而不是订阅的URL而没有“/”即EndPoint。

1 个答案:

答案 0 :(得分:2)

以下内容应该有效:

<httpHandlers>   
    <add verb="*" path="EndPoint" type="MyHandler/*" />
</httpHandlers>
相关问题