2011-02-01 95 views
0

我发送一个xml肥皂请求登录到我的crm ondemand网站,但我得到一个错误“http请求不包含格式良好的xml。试图解析它产生一个以下错误的东西XML-20108(致命错误)根元素缺失的开始“。我不知道该做什么:S ...我已经浪费了我2天的时间n无法取得任何进展..请帮助我解决这个问题!:(..肥皂请求中有错误的地方XML Soap请求crm ondemand从目标c

NSString *post = [NSString stringWithFormat:   
@"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \ 
< soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/ \"    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:enc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:ns9060=\"http://tempuri.org\"> \ 
< soap:Header> \ 
< wsse:Security soap:mustUnderstand=\"1\"> \ 
< wsse:UsernameToken> \ 
< wsse:Username>MyUserName</wsse:Username> \ 
< wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">MyPassword</wsse:Password> \ 
< /wsse:UsernameToken> \ 
< /wsse:Security> \ 
< /soap:Header> \ 
< soap:Body> \ 
< AccountQueryPage_Input xmlns=\"urn:crmondemand/ws/ecbs/account/10/2004\"> \ 
< ListOfAccount xmlns=\"urn:/crmondemand/xml/Account/Query\"> \ 
< Account> \ 
    < AccountName/> \ 
< /Account> \ 
< /ListOfAccount> \ 
< /AccountQueryPage_Input> \ 
< /soap:Body> \ 
< /soap:Envelope>"]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSURL *url = [NSURL URLWithString:@"https://secure-ausomxdsa.crmondemand.com/Services/Integration"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setValue:@"document/urn:crmondemand/ws/ecbs/account/10/2004:AccountQueryPage" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest setValue:@"application/soap+xml;charset=ISO-8859-1" forHTTPHeaderField:@"Current-Type"]; 
    [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPBody:postData];  


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

    if(theConnection) 
    { 
     status.text = @"Connection"; 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 

    } 

    } 

    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     status.text = @"didReceiveResponse"; 
     [webData setLength: 0]; 
    } 
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 

      status.text = @"didReceiveData"; 
      [webData appendData:data]; 
    } 
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
     status.text = @"didFailWithError";   
     [connection release]; 
     [webData release]; 
    } 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 

     NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
     status.text = nil; 
     status.text = loginStatus; 
     [loginStatus release]; 

     [connection release]; 
     [webData release]; 
    } 

    @end 
+0

老兄,请帮个忙,使用ASIHTTPRequest:http://www.google.com.au/url?sa=t&source=web&cd=1&ved=0CBgQFjAA&url=http%3A%2F%2Fallseeing-i.com%2FASIHTTPRequest %2F&ei = 2-VHTa_jLMmXcdSx5OIC&usg = AFQjCNFpUZprrMAY9mTk0aGzEzwSG8L9sg – 2011-02-01 10:52:18

+0

伙计......你能告诉我在我的代码中出了什么问题吗?im新客户cn浪费了2,3天的时间对我来说一个非常新的方法对我来说不是一件好事:S :( – Casper 2011-02-01 11:37:40

回答

3

似乎有至少两个问题的SOAP请求。

首先,元素标记有<后的空间。
例如,< soap:Envelope<soap:Envelope
全部删除前导空格起始标签和结束标签。

二,xmlns:soap的URI的结束引号之前尾随空间:

xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/ \" 
                 ^trailing space 

应该有关闭的引号之前没有尾随空间。

你可以使用类似http://www.w3schools.com/xml/xml_validator.asp的东西来验证你的xml。

这并不能保证请求将在这些修复后工作,但它会排除无效的XML,并希望让你进一步移动。