2010-09-09 136 views
0

我正在從iPhone連接到SOAP webservice。我無法連接網絡服務。這裏我包含了我的代碼:如何連接SOAP Web服務?

 NSString *soapMessage = [NSString stringWithFormat: 
     @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
     "<SOAP-ENV:Envelope \n" 
     "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n" 
     "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
     "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" 
     "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" 
     "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n" 
     "xmlns:mob=\"http://XXX.edu\">\n" 
     "<SOAP-ENV:Body> \n" 
     "<mob:Login>\n" 
     "<mob:username>User Name</mob:username>\n" 
     "<mob:password>Password</mob:password>\n" 
     "</mob:Login>\n" 
     "</SOAP-ENV:Body> \n" 
     "</SOAP-ENV:Envelope>"]; 



     NSURL *url = [NSURL URLWithString:@"https://YYY.edu/UserService.svc"]; 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

     NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

     [request addValue: @"Login" forHTTPHeaderField:@"SOAPAction"]; 

     [request addValue: @"text/xml; charset=\"utf-8" forHTTPHeaderField:@"Content-Type"]; 

     [request addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 

     [request setValue:@"https://YYY/UserService" forHTTPHeaderField:@"Host"]; 

     [request setHTTPMethod:@"POST"]; 

     [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

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


     WSDL URL : https://XXX/UserService.svc?wsdl 

     End point : https://XXX/UserService.svc 


     **WSDL XML format** 

     <wsdl:definitions name="UserService" targetNamespace="http://tempuri.org/"> 
     <wsdl:import namespace="http://YYY/" location="https://XXX/UserService.svc/basic?wsdl=wsdl1"/> 
     <wsdl:types/> 
     <wsdl:service name="UserService"> 
     <wsdl:port name="ZZZ" binding="i0:ZZZ"> 
     <soap:address location="https://XXX/UserService.svc"/> 
     </wsdl:port> 
     </wsdl:service> 
     </wsdl:definitions> 


     This is the XMl format and request input i had used. I cant connect with the web service. I got the error message (response) is either empty or "<h1>Bad Request (Invalid Hostname)</h1>". 

     where i am getting lack. Please any one help me. 

     Thanks in advance. 

回答

0

嘗試更改主機,在web服務中,您可以檢查主機標頭以與發佈Web服務的主機相匹配。可能將標題「Host:」參數設置爲正確的值可以解決問題。在主題參數中只放置主機:端口。