当我想使用SOAP使用Web服务时出现错误

时间:2012-10-22 13:48:13

标签: objective-c xcode web-services soap ios4

我创建一个Web服务(SampleService.asmx)并在其中放入一个返回字符串的方法(GetAllUserInfo2)。我在Visual Studio上测试并正常工作。

我使用下面的消息来消费它,我检查日志文件但发生错误

//Message
NSString *soapMessage=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
     "<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/\">\n"
     "<soap:Body>\n"
     "<GetCategory xmlns=\"http://jahanmir.ir/\" />\n"
     "</soap:Body>\n"
     "</soap:Envelope>";

    NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.jahanmir.ir/SampleService.asmx"]];
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];
    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    //[theRequest addValue:@"http://jahanmir.ir/GetAllUserInfo2" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:@"http://jahanmir.ir/SampleService.asmx?op=GetAllUserInfo2" forHTTPHeaderField:@"SOAPAction"];
    NSString *msgLength=[NSString stringWithFormat:@"%i",[soapMessage length]];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if(con)
        webData=[[NSMutableData data] retain];//webData is `NSMutableData` 

//Log
2012-10-22 17:39:50.533 WebServices[522:f803] data: <?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><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://jahanmir.ir/SampleService.asmx?op=GetAllUserInfo2.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

1 个答案:

答案 0 :(得分:0)

您的问题似乎就在这一行[theRequest addValue:@"http://jahanmir.ir/GetAllUserInfo2" forHTTPHeaderField:@"SOAPAction"];

您正在向根而不是asmx文件发出服务请求。这未经过测试,但我认为您希望刊登http://jahanmir.ir/SampleService.asmx?op=GetAllUserInfo2

的标题