通过XML连接Webservice

时间:2013-04-25 09:42:55

标签: objective-c xcode

NSURL *url = [NSURL URLWithString:@"http://192.168.1.56/AMSP/AMSPWS.asmx"];

NSString *soapMsg=[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>"
                   "<PostXMLStr xmlns=\"http://tempuri.org/\">"
                   "<cust>%@</cust>"
                   "<tran>%@</tran>"
                   "<ret>%@</ret>"
                   "<ppay>%@</ppay>"
                   "<recp>%@</recp>"
                   "<sCode>%@</sCode>"
                   "<companyShortName>%@</companyShortName>"
                   "<companyCode>%@</companyCode>"
                   "</PostXMLStr>"
                   "</soap:Body>"
                   "</soap:Envelope>",cust,trans,RETURNS,prepayment,receipt,spcode1,companyShortName,companyCode];


NSMutableURLRequest *requests = [NSMutableURLRequest requestWithURL:url
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                    timeoutInterval:5];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[requests setHTTPMethod:@"POST"];
[requests addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[requests addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[requests addValue:@"http://tempuri.org/PostXMLStr" forHTTPHeaderField:@"SOAPAction"];

[requests setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding ]];

NSURLResponse *response;
NSError *error = nil;

NSData   *data =[[NSMutableData alloc]init];
data = [NSURLConnection sendSynchronousRequest:requests returningResponse:&response error:&error];

NSString *stringSoap;

stringSoap = [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding];
NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];

我是通过目标C连接WebService的新手。我不知道如何检查XML是否连接到WebService。当我打印stringSoap时,我没有得到任何价值。我不知道这是调用XML的正确方法。如何检查XML是否与WebService连接。任何帮助将不胜感激。提前致谢。

1 个答案:

答案 0 :(得分:0)

您是新手,这将有助于您将SOAP服务转换为ObjectiveC sudzc.com
wsdl2objc