0

我一直在尝试同时使用soap和soap12,但错误似乎总是出现。我一直在这里寻求帮助,我“完全”像他们说的那样做,但它对我不起作用。我想如果有人可以阅读我的代码。我刚开始使用objective-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/WebService1.asmx?op=Login

怎么了?每次我尝试解决问题时,都会出现另一个问题。谁能明白为什么?

4

1 回答 1

0

此方法将创建参数以在服务中发送并获得响应

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

以下方法使用参数字典创建 SOAP 消息

-(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;
}

我希望它能够完成这项工作,因为我长期以来一直在使用它。祝你好运

于 2013-07-03T04:19:35.890 回答