如何使用Objective c在WCF服务中的方法中传递2个参数?

时间:2015-02-09 10:34:43

标签: objective-c iphone wcf soap

我的参数是

Request1 = <Request> <name>2</name> <pwd>1</pwd> </Request>

Request2 = value1

我的代码是

-(void)webRequestCommnunication
{
//Web Service Call
 NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <soap:Body><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request> <name>2</name> <pwd>1</pwd> </Request></Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];

NSURL *url = [NSURL URLWithString:@"http://localIPaddress/servicename1/servicename2/Method1.svc"];

theRequest = [NSMutableURLRequest requestWithURL:url];


NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/service1/Method1" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{

    NSLog(@"Connected....");
}
else
{
    NSLog(@"theConnection is NULL");
}
}

我想在Method1中传递Request1,Request2,但是它会抛出异常,因为格式化程序在尝试反序列化消息时引发了异常:反序列化操作请求消息体时出错&#39; Method1&#39;。结束元素&#39; Request1&#39;来自命名空间&#39; http://tempuri.org/&#39;预期。找到元素&#39;请求&#39;来自命名空间&#39; http://tempuri.org/&#39;。第1行,第276位......

任何人都可以建议我做错了吗?

1 个答案:

答案 0 :(得分:0)

终于有了解决方案!

在发送Request1参数时,&lt;,&gt;在Soap格式中不解析符号,只验证符号的父标记,修改后的代码为

-(void)webRequestCommnunication
{
//Web Service Call
 NSString *soapMessage = [NSString stringWithFormat:
                     @"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <soap:Body><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request1>&lt;Request&gt;&lt;name&gt;2&lt;/name&gt;&lt;pwd&gt;1&lt;/pwd&gt;&lt;/Request&gt;</Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];

NSURL *url = [NSURL URLWithString:@"http://localIPaddress/servicename1/servicename2/Method1.svc"];

theRequest = [NSMutableURLRequest requestWithURL:url];


 NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/service1/Method1" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{

   NSLog(@"Connected....");
}
else
{
  NSLog(@"theConnection is NULL");
}
}