Как отправить XML в виде строки в iphone SDK? - PullRequest
2 голосов
/ 18 августа 2011

Я пытаюсь вызвать веб-сервер, в котором есть параметр xml, но когда я запускаю код, вызов веб-службы не выполняется, но когда я использую некоторую случайную строку в качестве параметра, вызывается веб-служба. Пожалуйста, сообщите мне что не так с моим XML или есть другой способ передать XML.

    NSString *xml=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><R><Root><CriteriaID>11</CriteriaID><Rating/></Root><Root><CriteriaID>10</CriteriaID><Rating/></Root><Root><CriteriaID>9</CriteriaID></Root><Root><CriteriaID>8</CriteriaID><Rating/></Root><Root><CriteriaID>7</CriteriaID><Rating/></Root><Root><CriteriaID>6</CriteriaID><Rating/></Root></R>"];

    NSString *soapMsg = [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"
                         "<FullWomPrintsInsert xmlns=\"http://tempuri.org/\">\n"
                         "<xmlDoc>%@</xmlDoc>"
                         "</FullWomPrintsInsert>"
                         "</soap:Body></soap:Envelope>",xml];
    NSLog(@"SoapMsg=%@",soapMsg);




    NSString *club_url = [NSString stringWithFormat:@"http://abc"];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:club_url]];


    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    NSLog(@"Message Length..%@",msgLength);

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/FullWomPrintsInsert" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];


    if( theConnection )
    {
        webData = [[NSMutableData data] retain];



    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

1 Ответ

1 голос
/ 19 августа 2011

Я запускал ваш код на моем сервере с синхронизированным подключением на Mac, а не на iPhone:

NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:NULL];

поймал тело у Чарльза:

<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<FullWomPrintsInsert xmlns="http://tempuri.org/">
<xmlDoc><?xml version="1.0" encoding="utf-8"?><R><Root><CriteriaID>11</CriteriaID><Rating/></Root><Root><CriteriaID>10</CriteriaID><Rating/></Root><Root><CriteriaID>9</CriteriaID></Root><Root><CriteriaID>8</CriteriaID><Rating/></Root><Root><CriteriaID>7</CriteriaID><Rating/></Root><Root><CriteriaID>6</CriteriaID><Rating/></Root></R></xmlDoc></FullWomPrintsInsert></soap:Body></soap:Envelope>

Который должен соответствовать тому, что вы пытаетесь отправить.

Заголовки были:

POST /test/html HTTP/1.1
Host    x.com
User-Agent  TestXML (unknown version) CFNetwork/520.0.13 Darwin/11.1.0 (x86_64) (Macmini4%2C1)
Content-Length  664
Accept  */*
Content-Type    text/xml; charset=utf-8
SOAPAction  http://tempuri.org/FullWomPrintsInsert
Accept-Language en-us
Accept-Encoding gzip, deflate
Connection  keep-alive

Если хотите, я могу запустить его на симуляторе или на устройстве.

...