[objective-c]SOAP web 服务传递参数时不给出答复

标签: Xcode Objective-C
发布时间: 2017/3/27 2:53:20
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
This is SOAP WebService method:

<PostXMLStr xmlns="http://tempuri.org/">
      <cust>string</cust>
      <tran>string</tran>
      <ret>string</ret>
      <ppay>string</ppay>
      <recp>string</recp>
      <sCode>string</sCode>
      <companyShortName>string</companyShortName>
      <companyCode>string</companyCode>

This is what i calling SOAP:
<PostXMLStr xmlns=\"http://tempuri.org/\">\n"
    "      <cust><NEWCUSTOMERS><NEWCUSTOMER></NEWCUSTOMER></NEWCUSTOMERS></cust>\n"
    "      <tran><TRANS><TRAN></TRAN></TRANS></tran>\n"
    "       <ret><RETURNS><RETURN></RETURN></RETURNS></ret>\n"
    "       <ppay><PREPAYMENTS></PREPAYMENTS></ppay>\n"
    "       <recp><RECEIPTS><RECEIPT></RECEIPT></RECEIPTS></recp>\n"
    "       <sCode>BB</sCode>\n"
    "   <companyShortName>SAMINC</companyShortName>\n"
    "   <companyCode>01</companyCode>\n"

我有一些问题,当将参数传递给 SOAP WebService.I 找不到它。当我调用另一个 SOAP web 服务有没有给我答复的论点。我假设问题与传递 argument.any 帮助的东西提前会 appreciated.thanks。

解决方法 1:

我写了 soap_parser 类让人读的 SOAP 消息,它笔直漂亮的前执行,只是步骤和你通过这一问题。

步骤 1︰ 创建.h 文件 soap_parser.h 与以下代码并进行适当域更改

#error Set Your Request Domain & Webservice name

#define DOMAIN_URL      @"http://yourDomain.com/WebService/"
#define SERVICE_URL     DOMAIN_URL@"/iphoneservice.asmx"
#define TEMP_URL        @"http://tempuri.org"

#import <Foundation/Foundation.h>

@protocol soap_parser_delegate <NSObject>

@optional
-(void)receivedResponseWithStatusCode:(NSInteger)statusCode;
-(void)requestFailedWithError:(NSError *)err;
@required
-(void)dataReceivingCompleted:(NSMutableData *)data;

@end

@interface soap_parser : NSObject
{
    NSMutableURLRequest *soap_request;
    NSURLResponse *soap_response;
    NSMutableData *soap_responseData;

    NSString *currentAction;

    id <soap_parser_delegate>delegate;
}

@property (nonatomic, retain) NSMutableData *soap_responseData;
@property (nonatomic, retain) NSString *currentAction;
@property (nonatomic, retain) id <soap_parser_delegate>delegate;

#pragma mark - Initialize Parsing
-(void)startParsingWithAction:(NSString *)action andWithParams:(NSDictionary *)params;

#pragma mark - Create SOAP message
-(NSString *)createSoapMesssageFrom:(NSDictionary *)requestParam;

@end

步骤 2︰ 创建一个.m 文件 soap_parser.m 与下面的代码

#import "soap_parser.h"

@implementation soap_parser

@synthesize soap_responseData, currentAction, delegate;

#pragma mark - Initialize Parsing
-(void)startParsingWithAction:(NSString *)action andWithParams:(NSDictionary *)params
{
    self.currentAction = action;

    NSString *reqSOAPmsg = [self createSoapMesssageFrom:params];

    NSURL *url = [NSURL URLWithString:SERVICE_URL];

    soap_request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [reqSOAPmsg length]];

    [soap_request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [soap_request addValue: [NSString stringWithFormat:@"%@/%@",TEMP_URL,self.currentAction] forHTTPHeaderField:@"SOAPAction"];
    [soap_request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [soap_request setHTTPMethod:@"POST"];
    [soap_request setHTTPBody: [reqSOAPmsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *cn = [[NSURLConnection alloc] initWithRequest:soap_request delegate:self];
    [cn start];
}

#pragma mark - Create SOAP message
-(NSString *)createSoapMesssageFrom:(NSDictionary *)requestParam
{
    NSMutableString *soapMessage = [[NSMutableString alloc] init];

    [soapMessage appendFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
     "<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/\">\n"
     "<soap:Body>\n"
     "<%@ xmlns=\"http://tempuri.org/\">\n",self.currentAction];

    for(NSString *key in requestParam)
    {
        [soapMessage appendFormat:@"<%@>%@</%@>\n",key,[requestParam valueForKey:key],key];
    }
    [soapMessage appendFormat:@"</%@>\n"
     "</soap:Body>\n"
     "</soap:Envelope>",self.currentAction];

    NSLog(@"%@",soapMessage);
    return soapMessage;
}

#pragma mark - NSURLConnection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

    if([delegate respondsToSelector:@selector(receivedResponseWithStatusCode:)])
    {
        [delegate performSelector:@selector(receivedResponseWithStatusCode:) withObject:httpResponse];
    }
    self.soap_responseData = [[NSMutableData alloc] initWithCapacity:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.soap_responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if([delegate respondsToSelector:@selector(requestFailedWithError:)])
    {
        [delegate performSelector:@selector(requestFailedWithError:) withObject:error];
    }

    connection = nil;
    self.soap_responseData = nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if([delegate respondsToSelector:@selector(dataReceivingCompleted:)])
    {
        [delegate performSelector:@selector(dataReceivingCompleted:) withObject:self.soap_responseData];
    }
}

@end

第 3 步︰ #import "soap_parser.h"<soap_parser_delegate> 在你 ViewController .h 文件,要使 soap 调用。

你.m 文件中的第 4 步︰ 实施以下委托方法

#pragma mark - SOAP parser Delegate
-(void)receivedResponseWithStatusCode:(NSInteger)statusCode
{
    NSLog(@"Status Code : %d",statusCode);
}

-(void)requestFailedWithError:(NSError *)err
{
    NSLog(@"Failed : %@",[err description]);
}

-(void)dataReceivingCompleted:(NSMutableData *)data
{
    NSLog(@"Response Data Length : %d",data.length);
    NSString *responseString = [[NSString alloc] initWithBytes:[data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",responseString);
    #warning Do something with your Response Data
}

第 5 步︰ 创建要发送的 SOAP 请求,要调用你的 SOAP_WEBSERVICE 和休息很会照顾的数据词典。

NSDictionary *dictParams = [NSDictionary dictionaryWithObjectsAndKeys:@"CUST ID", @"cust",
                            @"Transaction ID",@"tran",
                            @"Ret Value",@"ret",
                            @"PPAY Value",@"ppay",
                            @"RECP Value",@"recp",
                            @"SCODE Value",@"sCode",
                            @"MY COMPANY LTD.",@"companyShortName",
                            @"COMPANY_007",@"companyCode", nil];

// Create Object of SOAP_Parser
soap_parser *objSOAP = [[soap_parser alloc] init];
// Set Delegate to self
[objSOAP setDelegate:self];
// Send Request with param Dictionary
[objSOAP startParsingWithAction:@"UserLogin" andWithParams:dictParams];
赞助商