5

xx我是 iPhone 新手。在我的应用程序中使用 Soap 方法导入和导出数据..虽然导入工作正常但导出不工作,soap Web 服务的响应为空..

//参数

NSString *cust=@"<NEWCUSTOMERS><NEWCUSTOMER></NEWCUSTOMER></NEWCUSTOMERS>"; 
    NSString *trans=@"<TRANS><TRAN></TRAN></TRANS>";
    NSString *RETURNS=@"<RETURNS><RETURN></RETURN></RETURNS>";
    NSString *prepayment=@"<PREPAYMENTS><PREPAYMENT></PREPAYMENT></PREPAYMENTS>";
    NSString *receipt=@"<RECEIPTS><RECEIPT></RECEIPT></RECEIPTS>";
    NSString *spcode1=@"BB";
    NSString *companyShortName=@"Sample Company Limited";
    NSString *companyCode=@"01";

// 肥皂网址:

    NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xxx.xx/SAAP/SOAP.asmx"];

//SOAP方法:

    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 alloc] initWithURL:url];
    NSLog(@"soapMsg= %d soapMsg=%@",soapMsg.length,soapMsg);
    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
    NSLog(@"msgLength=%@ ",msgLength);

    [requests addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [requests addValue:@"chunked" forHTTPHeaderField:@"transfer-coding"];
    [requests addValue:msgLength forHTTPHeaderField:@"Content-Length"];
     [requests setValue:@"http://tempuri.org/PostXMLStr" forHTTPHeaderField:@"SOAPAction"];
    [requests setHTTPMethod:@"POST"];

    [requests setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
   // [requests setHTTPBodyStream:dataStream];
    //[requests setTimeoutInterval:2000];
    NSLog(@"request=%@",requests);

    NSURLResponse *response;
    NSError *error = nil;

//强文本:

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

//[NSURLConnection connectionWithRequest:requests delegate:self];
   //NSURLConnection  *connection = [[NSURLConnection alloc] initWithRequest:requests delegate:self startImmediately:YES];
   //NSURLConnection  *connection=[NSURLConnection connectionWithRequest:requests delegate:self];


NSLog(@"requestdata=%@",data);
    stringSoap = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
    NSLog(@"data %@",headers);
    NSLog(@"error %@ data str %@  resulString=%@",error,stringSoap,resulString);

//soap方法的结果:有数据str是空响应..

 soapMsg=<?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><NEWCUSTOMERS><NEWCUSTOMER></NEWCUSTOMER></NEWCUSTOMERS></cust><tran><TRANS><TRAN></TRAN></TRANS></tran><ret><RETURNS><RETURN></RETURN></RETURNS></ret><ppay><PREPAYMENTS><PREPAYMENT></PREPAYMENT></PREPAYMENTS></ppay><recp><RECEIPTS><RECEIPT></RECEIPT></RECEIPTS></recp><sCode>BB</sCode><companyShortName>Sample Company Limited</companyShortName><companyCode>01</companyCode></PostXMLStr></soap:Body></soap:Envelope>
2013-03-18 14:27:05.151 MSP[6940:207] msgLength=679 
2013-03-18 14:27:05.152 MSP[6940:207] request=<NSMutableURLRequest http://xx.xxx.xxx.xx/SAAP/SOAP.asmx>
2013-03-18 14:27:05.410 MSP[6940:207] requestdata=<>
2013-03-18 14:27:05.411 MSP[6940:207] data {
    "Cache-Control" = private;
    "Content-Length" = 0;
    Date = "Mon, 18 Mar 2013 08:59:16 GMT";
    Server = "Microsoft-IIS/5.1";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
}
2013-03-18 14:27:05.412 MSP[6940:207] error (null) data str   resulString=(null)

请帮我...

4

1 回答 1

1

您的Transfer-Coding:chunked标头是否有可能使服务器感到困惑,因此它无法处理您的数据?您知道,Content-Length因此显然不需要分块发送数据,而且我认为无论如何您都无法做到这一点NSURLConnection

于 2013-05-04T02:31:43.057 回答