2013-02-12 71 views
-1

我问一个问题,跟着下面的方式(通过Jaybit建议): Pass username and password in URL for authentication错误,同时使URL请求iPhone

现在,我得到这个错误(堆栈跟踪):

2013-02-12 11:56:11.734 Calendar[4074:c07] didReceiveData 
2013-02-12 11:56:19.519 Calendar[4074:c07] receivedString:<?xml version="1.0"  encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. 
    at System.Xml.XmlTextReaderImpl.Throw(Exception e) 
    at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() 
    at System.Xml.XmlTextReaderImpl.ParseDocumentContent() 
    at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() 
    at System.Xml.XmlReader.MoveToContent() 
    at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() 
    at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() 
    at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() 
    at System.Web.Services.Protocols.SoapServerProtocol.Initialize() 
    at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) 
    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing) 
    --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /> </soap:Fault></soap:Body></soap:Envelope> 
(lldb) 

这里是我的代码:

- (IBAction)btnLoginClick:(id)sender 
{ 
//Call Calendar View 
if(self.viewController == nil) { 
    CalendarViewController *detailView = [[CalendarViewController alloc] initWithNibName:@"CalendarViewController" bundle:nil]; 
    self.viewController = detailView; 

} 
NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName]; 
NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword]; 


NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://URL/service1.asmx?"]]; 

NSString *postString = [NSString stringWithFormat:@"username=%@&password=%@",userName, passWord]; 
NSLog(@"%@",userName); 
NSLog(@"%@",passWord); 

NSData *postData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]]; 

//URL Requst Object 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:600]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody: postData]; 
appConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[self.appConnection start]; 

// tryed与异步...

/* NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 
    NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"receivedString:%@",receivedString); 

}];*/ 

[self.navigationController pushViewController:self.viewController animated:YES]; 

}

}

请help.thank。

+0

你检查你的网址在任何空白..?那么你把%20 – 2013-02-12 07:50:35

+0

请发表一个完整的问题。我不知道你想要什么。 – trojanfoe 2013-02-12 08:13:50

+0

嗨Nitin,从Safari正常工作的URL。 – bapi 2013-02-12 08:34:49

回答

1

我已经与之前请求肥皂URL的工作,下面的代码为我工作:

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" 
         "<YourServiceName xmlns=\"http://tempuri.org/\">\n" 
         "<username>%@</username>\n" 
         "<passowrd>%@</passowrd>\n" 
         "</YourServiceName>\n" 
         "</soap:Body>\n" 
         "</soap:Envelope>\n", userName, passWord 
         ]; 

NSURL *url = [NSURL URLWithString:@"http://URL/service1.asmx"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue: [NSString stringWithFormat:@"http://tempuri.org/%@",service] forHTTPHeaderField:@"SOAPAction"]; 
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
[theRequest setTimeoutInterval:10]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if(theConnection) 
{ 
    responseData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"theConnection is NULL"); 
} 
+0

谢谢。你的回答是什么“服务”? – bapi 2013-02-12 11:04:17

+0

请问任何人都可以告诉我这里“服务”是什么意思? [theRequest addValue:[NSString stringWithFormat:@“http://tempuri.org/%@”,service] forHTTPHeaderField:@“SOAPAction”];这是我的网络服务名称吗?如果是的话,什么是适当的格式? – bapi 2013-02-12 12:55:20

+0

是的,这是您的服务名称,它应该像YourServiceName。 – Scar 2013-02-12 13:11:54