C# 从我的web服务读取Http POST正文内容?(.net 3.5)
好吧,这让我快发疯了。我正在将Amazon SNS集成到我拥有的web服务中。Amazon只需向我指定的url发送一个包含大量json内容的HTTP post。我就是无法访问它C# 从我的web服务读取Http POST正文内容?(.net 3.5),c#,asp.net,.net,web-services,C#,Asp.net,.net,Web Services,好吧,这让我快发疯了。我正在将Amazon SNS集成到我拥有的web服务中。Amazon只需向我指定的url发送一个包含大量json内容的HTTP post。我就是无法访问它 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void HandleBounce() { notification = //I need to put the JSON content into here } Am
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HandleBounce()
{
notification = //I need to put the JSON content into here
}
Amazon只需访问url()。正在正确调用该方法。我只需要获取它在邮件中发送的数据。我该怎么做
编辑:
我最初尝试使用
[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());
}
}
让您的方法接受string类型的参数并将其称为json。调试您的应用程序并让我们知道发生了什么。可能是@Maritim的重复,我最初尝试这样做,但当我有一个字符串参数时,它根本不会调用该方法。不幸的是,由于请求是从Amazon发送的,我不知道响应是什么,但我想参数不是预期的或使用的。您有任何类型的请求可以检查吗?我怀疑post请求的内容类型可能不是您所期望的。@Maritim,我已将他们发送的假定请求添加到主要问题中。我注意到他们将类型编码为文本,而不是json。我需要做些不同的事情来处理吗?