iphone连接webservice的操作:
- (IBAction)connectwebservice:(id)sender {
NSString *username=@"s";
NSString *pwd=@"123456";
NSString *soapMessage1 = [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"
"<login xmlns=\"http://mspackage\">\n" //login是要执行的操作方法,xmlns的值是 命名空间。
"<i>1</i>\n" //后边是三个参数 i值 username值和pwd值
"<username>%@</username>\n"
"<pwd>%@</pwd>\n"
"</login>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",username, pwd];
//请求发送到的路径
//http://192.168.0.231:8080/axis2/services/mobileservice 是 服务地址
NSURL * url = [NSURL URLWithString:[@"http://192.168.0.231:8080/axis2/services/mobileservice" /stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage1 length]];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addValue: @"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://mspackage/login"forHTTPHeaderField:@"soapAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage1 dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *reponse;
NSError *error = nil;
//put数据之后的返回信息
UIAlertView *alert = nil;
if(error)
{
alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:[error description]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
}else
{
if(responseData)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSRange range = [responseString rangeOfString:@"username"];
if (range.length)
{
alert = [[UIAlertView alloc]
initWithTitle:@"登陆成功!"
message:[responseString description]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
}else
{
alert = [[UIAlertView alloc]
initWithTitle:@"用户名密码错误!"
message:[responseData description]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
}
}
}
[alert show];
}