我一直在尝试同时使用soap和soap12,但错误似乎总是出现。我一直在这里寻求帮助,我“完全”像他们说的那样做,但它对我不起作用。我想如果有人可以阅读我的代码。我刚开始使用objective-c,所以请详细解释一下。谢谢!
问题:
- 什么都没有发生,一切都停在:“收到的字节:0
- 服务器无法为请求提供服务,因为我不支持媒体
我的代码如下所示:
#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/WebService1.asmx?op=Login
怎么了?每次我尝试解决问题时,都会出现另一个问题。谁能明白为什么?