将SOAP响应转换为NSString

时间:2013-09-01 18:56:25

标签: objective-c web-services soap wsdl

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];
}

2 个答案:

答案 0 :(得分:0)

有两个可能出现的问题:

  1. 您永远不会显示webData属性的实例化。检查以确保它不是nil

  2. 您的请求似乎缺少某些空格。不应该如下吗?

    NSString *GetExRequest= @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                            "<soap:Envelope xmlns: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"
                            "<GetEXRate xmlns=\"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";
    

答案 1 :(得分:0)

我认为你不能在没有任何关于人的事先通知的情况下透露WSDL终点,我请求你删除你在上述问题中提到的WSDL链接。