xcode动态地向soap webservice发送参数

时间:2013-05-23 10:53:24

标签: objective-c xcode web-services soap

我写了一段代码,将参数发送到.Net soap web服务。问题是我可以将参数作为静态发送,这意味着参数值嵌入在soap消息中。我想用变量或其他东西替换嵌入的值。这样做的好方法是什么?

这是我的代码

 (IBAction)buttonClick:(id)sender {
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:
                         @"<?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"
                         "<PasswordCheck xmlns=\"http://tempuri.org/\">\n"
                         "<user>arif</user>"  // ****I want to remove this static value
                         "<password>arif</password>" // ****I want to remove this static value
                         "</PasswordCheck>"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"];
NSLog(soapMessage);

_lbl_result.text = soapMessage;


NSURL *url = [NSURL URLWithString:@"http://192.168.1.57/service1.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/PasswordCheck" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

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

//[nameInput resignFirstResponder];

}

1 个答案:

答案 0 :(得分:1)

试试这个

NSString *soapMessage = [NSString stringWithFormat:
     @"<?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"
     "<PasswordCheck xmlns=\"http://tempuri.org/\">\n"
     "<user>%@</user>" 
     "<password>%@</password>" 
     "</PasswordCheck>"
     "</soap:Body>\n"
     "</soap:Envelope>\n",userName,password];