制作URL请求iPhone时出错

时间:2013-02-12 07:34:28

标签: iphone ios objective-c url

我问了一个问题并按照以下方式(Jaybit建议): Pass username and password in URL for authentication

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

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];

//尝试使用Async ...

   /* 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];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
tempdata = data;
NSLog(@"didReceiveData");
if (!self.receivedData){
    self.receivedData = [NSMutableData data];
}
[self.receivedData appendData:data];
    }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {

    NSLog(@"connectionDidFinishLoading");
NSString *receivedString = [[NSString alloc] initWithData:tempdata encoding:NSUTF8StringEncoding];
NSLog(@"receivedString:%@",receivedString);

}

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:1)

之前我曾申请过肥皂网址,以下代码对我有用:

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");
}