c# - 从我的 Web 服务读取 Http POST 正文内容? (.net 3.5)

标签 c# asp.net .net web-services

好吧,这让我抓狂。我正在将 Amazon SNS 集成到我拥有的 Web 服务中。 Amazon 只是将带有一堆 json 内容的 HTTP post 发送到我指定的 url。我只是无法访问它。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HandleBounce()
{
    notification = //I need to put the JSON content into here
}

Amazon 只需访问 url ( http://test.com/webservice.aspx/HandleBounce )。该方法被正确调用。我只需要获取它在帖子中发送的数据。我该怎么做?

编辑:

我最初尝试使用

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HandleBounce(string json)
{
    notification = //I need to put the JSON content into here
}

但是当我这样做时,这个方法根本就不会被调用。至少当我删除参数时,该方法有效。

编辑 2:

这是亚马逊网站关于他们向我发送的请求:

POST / HTTP/1.1
x-amz-sns-message-type: SubscriptionConfirmation
x-amz-sns-message-id: 165545c9-2a5c-472c-8df2-7ff2be2b3b1b
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic
Content-Length: 1336
Content-Type: text/plain; charset=UTF-8
Host: example.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",
  "Token" : "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",
  "Timestamp" : "2012-04-26T20:45:04.751Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem"
  }

最佳答案

使用以下调整,我能够解决问题:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void HandleBounce() //This method is called from Amazon Simple Notification Service when we receive a bounce.
    {
        string notification = "";
        using (var stream = new MemoryStream())
        {
            var request = HttpContext.Current.Request;
            request.InputStream.Seek(0, SeekOrigin.Begin);
            request.InputStream.CopyTo(stream);
            notification = Encoding.UTF8.GetString(stream.ToArray());
        }
    }

关于c# - 从我的 Web 服务读取 Http POST 正文内容? (.net 3.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947645/

相关文章:

c# - UIHint 不使用 EditorTemplate

c# - 使用 visual studio 2012 恢复删除文件

无工作可做的 C# Windows 服务

C# 交互窗口丢失方法

c# - 如何在 MVC 应用程序中使用 Microsoft Graph 访问其他用户的资源(特别是电子邮件)?

c# - 特殊字符正则表达式

c# - 为绑定(bind)列表/跨线程和锁定问题异步添加值

asp.net - 使用 JSON.NET 在 ASP.NET 2.0 Web 应用程序上实现 JSON feed

asp.net - 用于从经过身份验证的端点下载大文件的跨浏览器解决方案

c# - 无法将类型 'System.IO.Stream' 隐式转换为 'Java.IO.InputStream'