开发者

How to send an xml as a string in iphone sdk?

I am trying to call a web servive in which there is xml paramater but when i run the code the web service call is not made but when i use some random string as a paramater then the web service gets called.Please tell me what is wrong with my xml or is there other way to pass 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: @"te开发者_StackOverflow社区xt/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");
    }


I ran your code to my server with a sync connection on a Mac, not an iPhone:

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

Caught the body in Charles:

<?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>

Which should match what you are trying to send.

The headers were:

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

If you want I can run it in the simulator or on a device.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜