设计NSObject类来调用web方法

时间:2012-05-01 01:54:16

标签: nsxmlparser nsobject

我在NSObject继承的单独类中编写post方法,其中我发送两个参数,例如用户名和密码以及viewcontroller作为委托的引用。 我该如何在这个类中构造初始化方法。通常我使用本机解析器在这个类中执行解析。

到目前为止,我一直这样做:

-(void)initWith:(NSString *)username password:(NSString*)password delegate:(id<name>)delegate

{

NSString *soapMessage = [[NSString alloc] initWithFormat:

                         @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         "<soapenv:Envelope \n"
                         "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
                         "xmlns:tem=\"http://tempuri.org/\"> \n"
                         "<soapenv:Header/>\n"
                         "<soapenv:Body>\n"
                         "<tem:Login>\n"

                         "<tem:username>%@</tem:username>\n"

                         "<tem:password>%@</tem:password>\n"

                         "</tem:Login>\n"

                         "</soapenv:Body>\n"

                         "</soapenv:Envelope>\n",hisUDID,myUDID];



NSURL *url = [NSURL URLWithString:kBaseURL];               
// NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *msgLength = [[NSString alloc] initWithFormat:@"%d", [soapMessage length]];          
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
[theRequest addValue: @"http://tempuri.org/XXXXX/Login" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];     
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[msgLength release];
[soapMessage release];

[theRequest release];
theRequest=nil;

if(theConnection)
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"The Connection is NULL");
}

}

一般来说,我已经看到初始化程序具有返回类型id并且它初始化了一些需要的sata结构...请告诉它,因为我想学习构建init方法的好方法!!

0 个答案:

没有答案