0

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

这是我的代码

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

}

4

1 回答 1

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];
于 2013-05-23T11:03:33.967 回答