无法从目标c中使用我的Web服务

时间:2013-07-02 21:17:15

标签: .net objective-c web-services

我一直在尝试使用肥皂和肥皂12,但错误似乎总是出现。我一直在这里寻找帮助,我“完全”像他们说我应该这样做,但它对我不起作用。我想如果有人能读我的代码。我刚开始使用objective-c所以请详细解释。谢谢!

问题:

  1. 什么都没发生,一切都停在:“收到的字节:0
  2. 服务器无法为请求提供服务,因为我不支持该媒体
  3. 我的代码如下:

    #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

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

1 个答案:

答案 0 :(得分: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);
}

Foloowing Method使用Paramter Dictionary

创建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;
}

我希望它会在我使用它的时候做好工作。祝你好运