【发布时间】:2013-09-01 18:56:24
【问题描述】:
NSString *GetExRequest=[NSString stringWithFormat:
@"<?xmlversion=\"1.0\"encoding=\"utf-8\"?>\n"
"<soap:Envelopexmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetEXRatexmlns=\"Web Services\">\n"
"<AGENT_CODE>xyzzyx</AGENT_CODE>\n"
"<USER_ID>12345689</USER_ID>\n"
"<PASSWORD>dkasdja</PASSWORD>\n"
"<AGENT_SESSION_ID>xyz1234</AGENT_SESSION_ID>\n"
"<TRANSFERAMOUNT>1000</TRANSFERAMOUNT>\n"
"<PAYMENTMODE>c</PAYMENTMODE>\n"
"<CALC_BY>p</CALC_BY>\n"
"<PAYOUT_AGENT_ID>20100008</PAYOUT_AGENT_ID>\n"
"<PAYOUT_COUNTRY>Bangladesh</PAYOUT_COUNTRY>\n"
"</GetEXRate>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSLog(@"%@",GetExRequest);
NSURL *url = [NSURL URLWithString:@"https://www.prabhuusa.com/SendWsv2/txnservice.asmx?wsdl"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [GetExRequest length]];
[theRequest addValue:@"www.prabhuusa.com" forHTTPHeaderField:@"Host"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"WebServices/GetEXRate" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [GetExRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
我正在尝试通过发送此 SOAP 请求来使用此 WSDL 定义。我想将响应存储到一个字符串并解析它。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [_webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [_webData mutableBytes] length:[_webData length] encoding:NSUTF8StringEncoding];
NSLog(@"WEBDATA::: %@",theXML);
}
但是响应字符串是空白的。我使用我使用 SOAP 客户端发送的 SOAP 请求检查了此 Web 服务并收到了 XML 响应。但我无法在目标 C 中得到响应。请给我一个解决方案
我的 didRecieveResponse 和 didRecieveData 方法中有这个
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webData appendData:data];
}
【问题讨论】:
-
您是否曾经将
_webData初始化为[NSMutableData data](用于检查didReceiveResponse)?您是否将数据附加到didReceiveData中?在connectionDidFinishLoading中,_webData是长度为零的NSMutableData,还是nil? (顺便说一句,我通常使用initWithData而不是initWithBytes,因为它看起来更直观,但这不是问题的根源。) -
对不起,我也在这里添加了我的 'didRecieveResponse' 和 'diRecieveData' 代码。如果我弄乱了 SOAP ACTION 标头,我会收到 XML 格式的错误响应,所以我猜接收端是正确的,发送请求时我可能做错了什么?
-
@YugeshShresta 您仍然没有显示实例化
webData的位置(如果您收到nil的响应,这是一个典型的问题)。但是,如果您收到响应,并且它报告错误,那么它可能在您的请求中。例如,我注意到您有一个开放标签<GetEXRatexmlns="Web Services">,我认为应该是<GetEXRate xmlns="Web Services">。但我真的不知道是否可能存在其他问题(请求的性质可能会有所不同,我不知道您的服务在寻找什么)。 -
另外
<soap:Envelopexmlns:xsi="...">看起来很可疑。也许<soap:Envelope xmlns:xsi="...">。<?xmlversion="1.0"encoding="utf-8"?>应该是<?xml version="1.0" encoding="utf-8"?>。看起来你已经删除了很多空格。 -
另外,代理代码不应该是
xyzzy吗? (开个玩笑:只是透露我的misspent childhood。)
标签: objective-c web-services soap wsdl