将数据从ios应用程序传递到C#Web服务

时间:2013-11-03 06:12:54

标签: c# ios web-services afnetworking afhttpclient

我正在尝试开发这个可以与本地网络下的C#Web服务进行通信的iOS应用程序。我正在使用AFNetworking来实现沟通。现在,我已经在this tutorial的帮助下从Web服务中读取了xml数据,这意味着我的应用程序可以连接到Web服务。但我真正想要做的是,将数据从应用程序传递到Web服务。我写了一些示例代码:

AFmyAPIClient *myClient = [AFmyAPIClient sharedClient];
NSMutableDictionary *requestArray = [NSMutableDictionary new];
NSString *details = [_listOfItems description];
float tempF= [_listOfItems totalPrice];
NSString *price = [NSString stringWithFormat:@"%f",tempF];
int tempI = 1;
NSString *table = [NSString stringWithFormat:@"%u",tempI];

[requestArray setObject:details forKey:@"details"];
[requestArray setObject:price forKey:@"price"];
[requestArray setObject:table forKey:@"table"];

[myClient postPath:@"setOrderDetails" parameters:requestArray success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"success!");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

构建并运行此应用程序后,它会生成以下错误消息:

2013-11-03 16:25:48.654 HLR[16149:70b] <?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://localhost/service/Service1.asmx">3</int>
2013-11-03 16:25:48.844 HLR[16149:70b] Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x8b5b140 {NSLocalizedRecoverySuggestion=Missing parameter: orderDetails.
, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8b5bb70> { URL: http://10.0.0.111/service/Service1.asmx/setOrderDetails }, NSErrorFailingURLKey=http://10.0.0.111/service/Service1.asmx/setOrderDetails, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8b4e910> { URL: http://10.0.0.111/service/Service1.asmx/setOrderDetails } { status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 34;
    "Content-Type" = "text/plain; charset=utf-8";
    Date = "Sun, 03 Nov 2013 05:25:34 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
} }}

在我的C#Web服务方面,我有以下Web方法:

[Web Method]
public void setOrderDetails(String orderDetails)
{
         //store orderDetails into a text file.

}

我是iOS开发的新手,我不明白在调用此方法时我真正传递给Web服务的是什么:

[myClient postPath:@"setOrderDetails" parameters:requestArray success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"success!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];

我是否只是将NSDictionary作为参数传递给C#Web服务?如果是这样的话,我可以在Web服务端使用这个NSDictionary做什么?

我不知道这是否是将数据传递到Web服务的正确方法。我真正想要做的是将一些字符串传递给Web服务,我该如何实现呢?

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

您将XML内容作为URL参数传递。您需要在请求的正文中传递它。以下是使用AFNetworking发布XML的几个问题:

Example on using AFNetworking to post XML?

AFNetworking - Making an XML POST request to REST service? How to form NSDictionary parameters?