我正在开发能够访问soap web服务的iphone和android应用程序。在android中我使用ksoap2第三方库变得更容易。但是当我来到iphone时,我没有任何库或内置方法。
我在网上浏览时获得了链接http://iphonedevsdk.com/forum/iphone-sdk-development/39819-how-to-call-wcf-service.html。我尝试了相同的程序,但我得到了状态代码400(由于语法错误,服务器无法理解该请求。客户端不应该在没有修改的情况下重复请求。)。
请求格式
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/Logindetails
</Action>
</s:Header>
<s:Body>
<Logindetails xmlns="http://tempuri.org/">
<value> {"SessionId":"dfg45fsg","IpAddress":"192.168.0.1","UserName":"tiru123","Password":"ku204","MacAddress":"192:158:012:45","IMEINo":"testno","Location":"Adyar"}</value>
</Logindetails>
</s:Body>
</s:Envelope>
响应格式
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="57d29115-decc-4d40-8716-2495ee9d9c20" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">a5d15743-7290-41fd-a7e6-47f8a1dc23d6</ActivityId>
</s:Header>
<s:Body>
<LogindetailsResponse xmlns="http://tempuri.org/">
<LogindetailsResult>{"CollectorId":0,"IMEINo":null,"IpAddress":null,"IsChangePassword":false,"IsRefund":false,"Location":null,"MacAddress":null,"MerchantGroup":"Direct","MerchantId":264,"Password":null,"Roles":null,"SessionId":null,"UserId":450,"UserName":"tirupoomi123","Usergroup":"Merchant"}
</LogindetailsResult>
</LogindetailsResponse>
</s:Body>
</s:Envelope>
我的iphone代码
NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<s:Header>\n"
"<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">\n"
"http://tempuri.org/IService1/Logindetails\n"
" </Action>\n"
" </s:Header> \n"
"<s:Body>\n"
" <Logindetails xmlns=\"http://tempuri.org/\">\n"
" <value>\n"
"{\"SessionId\":\"dfg45fsg\",\"IpAddress\":\"192.168.0.1\",\"UserName\":\"tiru123\",\"Password\":\"ku204\",\"MacAddress\":\"192:158:012:45\",\"IMEINo\":\"testno\",\"Location\":\"Adyar\"}\n"
"</value>\n"
" </Logindetails> \n"
"</s:Body> \n"
"</s:Envelope>";
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://192.168.0.189/Service1.svc?wsdl"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/IService1/Logindetails" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection) {
webData = [[NSMutableData data] retain];
}
else {
NSLog(@"theConnection is NULL");
}
如果有人有任何解决方案,请帮助我
以上代码在普通的wsdl Web服务中运行良好。但不是在wcf服务中。
我认为Web服务存在问题。
如果有任何机构有iphone访问的样本wcf服务请与我分享