2013-07-02 43 views
0

我一直在嘗試使用soap和soap12,但錯誤似乎總是出現。我一直在這裏尋找一些幫助,我確實「像他們說我應該這樣做,但它不適合我。我想如果有人可以閱讀我的代碼。我剛開始使用Objective-C,請詳細解釋。謝謝!不能使用我的web服務從目標c

的問題:

  1. 什麼也沒有發生,一切都停止在「接收字節數:0
  2. 因爲媒體我不支持的

我的代碼看起來像這樣的服務器不能服務請求:

#import "ViewController.h" 


@interface ViewController() { 

} 

@end 

@implementation ViewController 

@synthesize username, password, conWebData, soapResults, xmlParser; 


- (IBAction)Login:(UIButton *)sender { 


    NSString *soapMessage = [NSString stringWithFormat: 
     @"<?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" 
        "<Login xmlns=\"http://zermattproject.azurewebsites.net/WebService1.asmx\">\n" 
        "<userName>%@</userName>\n" 
        "<password>%@</password\n" 
        "</Login>\n" 
        "</soap:Body>\n" 
          "</soap:Envelope>\n", username.text, password.text]; 


    soapMessage = [NSString stringWithFormat:soapMessage, username.text]; 
    soapMessage = [NSString stringWithFormat:soapMessage, password.text]; 
    NSData *envelope = [soapMessage dataUsingEncoding:NSUTF8StringEncoding]; 
    NSString *url = @"http://zermattproject.azurewebsites.net/WebService1.asmx"; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 

    /*[theRequest addValue:@"http://zermattproject.azurewebsites.net/WebService1.asmx/Login" forHTTPHeaderField:@"SOAPAction"];*/ 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:envelope]; 
    [theRequest setValue:@"text/xml; charset=utf-8; action=http://zermattproject.azurewebsites.net/WebService1.asmx?op=Login" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest setValue:[NSString stringWithFormat:@"%d", [envelope length]] forHTTPHeaderField:@"Content-Length"]; 

    NSLog(@"%@", soapMessage); 

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

    if(theConnection) 
    { 
     conWebData = [NSMutableData data]; 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 
} 


- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [conWebData setLength:0]; 
} 

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data 
{ 
    [conWebData appendData:data]; 
} 

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"ERROR with theConnection"), error.localizedDescription, 
    [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{ 
    NSLog(@"DONE. Received Bytes: %d", [conWebData length]); 
    NSString *theXML = [[NSString alloc]initWithBytes:[conWebData mutableBytes] length:[conWebData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", theXML); 

    xmlParser = [[NSXMLParser alloc]initWithData: conWebData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities:YES]; 
    [xmlParser parse]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 


     if(!soapResults) 
     { 
      soapResults = [[NSMutableString alloc]init]; 
     } 
     recordResults = TRUE; 

} 

- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string 
{ 
    if(recordResults) 
    { 
     [soapResults appendString: string]; 
    } 
} 

- (void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if([elementName isEqualToString:@"LoginResult"]) 
    { 
     recordResults = FALSE; 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:soapResults delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     soapResults = nil; 
    } 
} 



@end 

而我的網絡服務:zermattproject.azurewebsites.net/Web Service1.asmx?op =登錄

什麼是錯?每次嘗試解決問題時都會出現另一個問題。任何人都能看到爲什麼

回答

0

這個方法將創建paramters的服務來發送和獲取響應

- (void)sendRequest 
{ 
    // Create Paramters to Send 
    NSDictionary *dictParams = [NSDictionary dictionaryWithObjectsAndKeys:@"Shree",@"UserName",@"s123",@"Password", nil]; 

    // Create SOAP Message by calling method with action name 
    NSString *reqSOAPmsg = [self createSoapMesssageFrom:dictParams andAction:@"Login"]; 

    NSURL *url = [NSURL URLWithString:@"http://www.zermattproject.azurewebsites.net/WebService1.asmx"]; // write your webservice domain address upto .asmx file 

    NSURLRequest *soap_request = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [reqSOAPmsg length]]; 

    [soap_request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [soap_request addValue: [NSString stringWithFormat:@"%@/%@",TEMP_URL,self.currentAction] forHTTPHeaderField:@"SOAPAction"]; 
    [soap_request addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [soap_request setHTTPMethod:@"POST"]; 
    [soap_request setHTTPBody: [reqSOAPmsg dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSError *error; 
    NSURLResponse *response; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:soap_request returningResponse:&response error:&error]; 
    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
    NSLog(@"RESPONSE : %@",data); 
} 

Foloowing方法創建使用放慢參數字典

-(NSString *)createSoapMesssageFrom:(NSDictionary *)requestParam andAction:(NSString *)action 
{ 
    NSMutableString *soapMessage = [[NSMutableString alloc] init]; 

    [soapMessage appendFormat:@"<?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" 
    "<%@ xmlns=\"http://tempuri.org/\">\n",action]; 

    for(NSString *key in requestParam) 
    { 
     [soapMessage appendFormat:@"<%@>%@</%@>\n",key,[requestParam valueForKey:key],key]; 
    } 
    [soapMessage appendFormat:@"</%@>\n" 
    "</soap:Body>\n" 
    "</soap:Envelope>",self.currentAction]; 

    NSLog(@"%@",soapMessage); 
    return soapMessage; 
} 

我希望它會做的工作,因爲我用它SOAP消息很久以來。好運

+0

謝謝!我現在沒有得到任何錯誤,但我只是得到了soapMessage和「Response:」的控制檯 – user2544317