【问题标题】:Post JSON to ASP.NET from iOS从 iOS 将 JSON 发布到 ASP.NET
【发布时间】:2012-07-18 03:32:00
【问题描述】:

我正在尝试将 JSON 数据从 iPhone 应用程序发送到我的 asp.net MVC 服务器。我的服务器中的方法如下所示:

[HttpPost]
public String MyMethod(Stream anUpload) { ... }

这是从我的应用程序中发布 JSON 的代码:

NSString *theURL = [NSString stringWithFormat:@"http://192.168.1.103/MyServer/MyMethod"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[theRequest setHTTPMethod:@"POST"];

// Serialize my data.
NSData *theData = [NSJSONSerialization dataWithJSONObject:theList options:kNilOptions error:nil];

[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [theData length]] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:theData];

但是,当我发送请求时,我只是从服务器收到一条错误消息,MyMethod() 永远不会被调用。我假设 ASP 接受 POST 数据存在问题。有什么想法吗?

编辑:在下面回答

【问题讨论】:

    标签: ios asp.net-mvc json post


    【解决方案1】:

    我找到了答案:

    我只需要将服务器端功能更改为

    public String MyMethod(List<string> aList) { ... }

    ModelBinder 试图为我反序列化 JSON,它不喜欢我的参数是 Stream。

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 2013-07-16
      • 2013-01-23
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多