2011-04-27 214 views
1

我正在嘗試使用僅返回日期(測試)的soap web服務。但我無法連接到Web服務。我只接收來自Web服務的wsdl,但我無法獲得所需的數據。以下是我的目標代碼c在iphone上製作肥皂請求

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" 
"<getDate xmlns:type=\"xsd:string\">\n" 
"</getDate>\n" 
"</soap:Body>\n" 
"</soap:Envelope>\n"; 
NSURL *url = [NSURL URLWithString:@"http://10.1.6.5/gnosis2/public/wslogin/"]; 
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[req addValue:@"http://10.1.6.5/gnosis2/public/wslogin/" forHTTPHeaderField:@"SOAPAction"]; 
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
[req setHTTPMethod:@"POST"]; 
[req setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 

我想從Web服務中使用getDate方法。有人可以幫幫我嗎?

編輯
上面的代碼給我下面的XML是一樣的瀏覽器中查看WSDL:

<?xml version="1.0"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://10.1.6.5/gnosis2/public/wslogin/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="MyWebService" targetNamespace="http://10.1.6.5/gnosis2/public/wslogin/"><types><xsd:schema targetNamespace="http://10.1.6.5/gnosis2/public/wslogin/"/></types><portType name="MyWebServicePort"><operation name="getDate"><documentation>Get the server date and time</documentation><input message="tns:getDateIn"/><output message="tns:getDateOut"/></operation><operation name="getAgeString"><documentation>Get a nicely formatted string of a person's age</documentation><input message="tns:getAgeStringIn"/><output message="tns:getAgeStringOut"/></operation></portType><binding name="MyWebServiceBinding" type="tns:MyWebServicePort"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="getDate"><soap:operation soapAction="http://10.1.6.5/gnosis2/public/wslogin/#getDate"/><input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></input><output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></output></operation><operation name="getAgeString"><soap:operation soapAction="http://10.1.6.5/gnosis2/public/wslogin/#getAgeString"/><input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></input><output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></output></operation></binding><service name="MyWebServiceService"><port name="MyWebServicePort" binding="tns:MyWebServiceBinding"><soap:address location="http://10.1.6.5/gnosis2/public/wslogin/"/></port></service><message name="getDateIn"/><message name="getDateOut"><part name="return" type="xsd:string"/></message><message name="getAgeStringIn"><part name="name" type="xsd:string"/><part name="age" type="xsd:int"/></message><message name="getAgeStringOut"><part name="return" type="xsd:string"/></message></definitions> 

感謝
潘卡

+0

感謝張貼SOAP消息字符串格式,它只是解決了我的格式化SOAP請求:) – 2012-11-29 07:22:22

回答

1
+0

的問題,我一直在使用這些文章試圖幾乎完成了在這裏所說的一切,但它仍然不能正常工作 – pankaj 2011-04-27 07:26:59

+0

@ pankaj,你在哪裏卡住了。我的朋友沒有正確地回答你的問題。 – Tirth 2011-04-27 07:29:42

+0

我收到了從服務器返回的完整wsdl,但我沒有從服務器獲得所需的響應。 – pankaj 2011-04-27 07:53:59

0

以下代碼塊幫助我使用SOAP請求處理Web服務。

-(void)getWebServiceContent { 

    NSURL *url = [NSURL URLWithString:@"http://url_value_here"]; 
    NSString *soapBody = @"soap_body_here"; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPMethod:@"POST"]; //GET 
    [request addValue:@"Value_to_be_added" forHTTPHeaderField:@"SOAPAction"]; 
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject{ 

     NSLog(@"Success : Content : %@",[operation responseString]); 
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) { 

     NSLog(@"Failed"); 
    })]; 

    [operation start]; 
}