0

soap と soap12 の両方を使用しようとしましたが、常にエラーが表示されるようです。私はここで助けを求めて探していて、彼らが私がすべきだと言うように「正確に」やっていますが、私にはうまくいきません. 誰かが私のコードを読むことができれば幸いです。私はobjective-cを始めたばかりなので、詳しく説明してください。ありがとう!

問題点:

  1. 何も起こらず、すべてが停止します: "Received bytes: 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

そして私の Web サービス: 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 に答える