2015-02-09 55 views
0

我的參數是如何使用Objective c在WCF服務的方法中傳遞2個參數?

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"); 
} 
} 

我想通過Request1,請求2中方法1,但它會拋出異常,因爲格式化拋出一個異常,而試圖反序列化消息:錯誤反序列化操作'Method1'的請求消息體。預期的命名空間'http://tempuri.org/'的結尾元素'Request1'。從命名空間'http://tempuri.org/'找到元素'Request'。 1號線,位置276 ..

任何人都可以建議我做錯了什麼?

回答

0

終於有了解決方案!

雖然發送Request1參數<,>符號不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"); 
} 
} 
相關問題