5
xxI是iPhone新手。在我的应用肥皂方法 使用导入和导出data..while进口工作不错,但出口不工作从SOAP网络服务的响应是空的..ios-Soap方法通过http post请求调用,但响应为空
//参数
NSString *[email protected]"<NEWCUSTOMERS><NEWCUSTOMER></NEWCUSTOMER></NEWCUSTOMERS>";
NSString *[email protected]"<TRANS><TRAN></TRAN></TRANS>";
NSString *[email protected]"<RETURNS><RETURN></RETURN></RETURNS>";
NSString *[email protected]"<PREPAYMENTS><PREPAYMENT></PREPAYMENT></PREPAYMENTS>";
NSString *[email protected]"<RECEIPTS><RECEIPT></RECEIPT></RECEIPTS>";
NSString *[email protected]"BB";
NSString *[email protected]"Sample Company Limited";
NSString *[email protected]"01";
//肥皂网址:
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xxx.xx/SAAP/SOAP.asmx"];
// SOAP Methos:
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)
请帮我...
如果您发现请求中的内容长度仍然为零。我建议你为所有请求使用SetValue,而不是像下面那样使用addValue。 [请求setValue:@“text/xml; charset = utf-8”forHTTPHeaderField:@“Content-Type”]; [请求setValue:@“chunked”forHTTPHeaderField:@“transfer-coding”]; [请求setValue:msgLength forHTTPHeaderField:@“Content-Length”];请尝试一下,让我知道。 – Prav 2013-04-29 08:36:38