SOAP Web API请求返回HTML而不是XML

时间:2015-04-05 06:09:10

标签: ios asp.net iphone web-services soap

我是SOAP API的新手。

我使用以下代码访问我的API。

NSString *stringURL=[NSString stringWithFormat:@"http://tennispro.co.in/services.asmx"];

// Allocate and initialize an NSURLConnection with the given request and delegate. The asynchronous URL load process is started as part of this method.

NSString *soapMessage = [NSString stringWithFormat:@"<?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>"
                         "<ReturnSyncMasterData xmlns=\"http://tempuri.org/\">"
                         "<datetime>%@</datetime>"
                         "</ReturnSyncMasterData>"
                         "</soap:Body>"
                         "</soap:Envelope>",@""];


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]];

[request addValue:@"text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"2159" forHTTPHeaderField:@"Content-Length"];
[request addValue:@"private, max-age=0" forHTTPHeaderField:@"Cache-Control"];
[request addValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
[request addValue:@"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];

theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

if (theConnection) {
    receivedData = [[NSMutableData alloc] init];
}

但作为回应,我得到HTML而不是XML。我想访问ReturnSyncMasterData肥皂操作,但我无法调用它。

1 个答案:

答案 0 :(得分:0)

将内容类型设置为text / xml

[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"2159" forHTTPHeaderField:@"Content-Length"];
[request addValue:@"private, max-age=0" forHTTPHeaderField:@"Cache-Control"];
[request addValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
[request addValue:@"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];

theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

if (theConnection) {
    receivedData = [[NSMutableData alloc] init];
}