【问题标题】:Fault string error while accessing web service in iOS在 iOS 中访问 Web 服务时出现错误字符串错误
【发布时间】:2014-01-03 11:22:33
【问题描述】:

我正在我的 iPhone 应用程序中访问 xml Web 服务。但我无法从服务器获取适当的数据。我在 NSURLConnection 的 connectionDidFinishLoading 委托方法中遇到以下错误。

soap:服务器服务器 无法处理请求。 ---> 对象引用未设置为 对象的实例。

以下是我的代码:

NSString *deviceID = [OpenUDID value];

NSString *soapMsg = [NSString stringWithFormat:
                     @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                     "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                     "<soap:Header>"
                     "<AuthHeader xmlns=\"http://tempuri.org/\">"
                     "<Username>admin</Username>"
                     "<Password>admin</Password>"
                     "</AuthHeader>"
                     "</soap:Header>"
                     "<soap:Body>"
                     "<WsGetAllDrugDetailsXML xmlns=\"http://tempuri.org/\">"
                     "<UserId>%d</UserId>"
                     "<IMEI>%@</IMEI>"
                     "</WsGetAllDrugDetailsXML>"
                     "</soap:Body>"
                     "</soap:Envelope>",[Appdelegate.UserID intValue],deviceID];


//---print it to the Debugger Console for verification---
NSLog(@"%@", soapMsg);

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:Appdelegate.wsURL];

//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/WsGetAllDrugDetailsXML" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {

    webData = [[NSMutableData data] retain];
}

我无法预测我这边出了什么问题。或者是服务器端的错误,我没有收到数据。 请帮助我。在此先感谢!

【问题讨论】:

  • 使用一些肥皂客户端测试您的网络服务。您可以检查问题出在网络服务还是您的代码中..
  • 你可以在soapui中测试你的wsdl或者在webbrowser中直接执行endpoint url来检查你的服务是否工作。我对 xcode 不熟悉,所以无法为您的客户提供帮助。

标签: ios web-services soap nsurlconnection nsurlsession


【解决方案1】:

您的Content-Length 值不正确:

长度必须以字节为单位。 NSString 的length 方法返回UTF-16 编码单元的数量。

您可以按如下方式修复它:

NSData* bodyData = [soapMsg dataUsingEncoding:NSUTF8StringEncoding];
[req addValue:[NSString stringWithFormat:@"%d",(int)[bodyData length]] forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:bodyData];

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2020-04-27
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多