我试图调用一个web服务器,其中有xml参数,但是当我运行代码时,没有进行Web服务调用,但是当我使用一些随机字符串作为参数时,Web服务就会被调用。请告诉我我的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");
}
答案 0 :(得分:1)
我在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
如果您愿意,我可以在模拟器或设备上运行它。