原理 根据 WebService 提供的 请求示例进行编写!
不多说 直接放代码 下面是关键的代码:
#define my_web_address @"http://192.168.1.133/"
#define my_web_service @"CatWebService/CatUserService.asmx"
#define my_web_namespace @"http://www.xxxx.com/"
//返回Soap Body
+(NSString *)getSoapBody:(NSString *)funcationName parameter:(NSString *)content
{
if(content == nil)
{
content = @"";
}
return [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\"><soap:Body>"
"<%@ xmlns=\"%@\">"
"%@"
"</%@>"
"</soap:Body></soap:Envelope>",funcationName,my_web_namespace,content,funcationName];
}
//返回SoapBody URL Request 信息
+(NSMutableURLRequest *)getRequest:(NSString *)funcationName parameter:(NSString *)pars
{
NSString* message = [self getSoapBody:funcationName parameter:pars];
NSString* mesLenth = [NSString stringWithFormat:@"%d",message.length];
NSString* funurl = [NSString stringWithFormat:@"%@%@",my_web_namespace,funcationName];
NSURL* url = [NSURL URLWithString:[self getWSDLURL]];
// NSLog(@"url is :%@",url);
// NSLog(@"SOAPAction is %@: ",funurl);
// NSLog(@"SoapBoay:%@",message);
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:funurl forHTTPHeaderField:@"SOAPAction"];
[request addValue:mesLenth forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
return request;
}
其实 有了上面的两个方法 差不多
用NSURLConnection 就可以访问 WebService 了
NSMutableURLRequest* request = [WebServiceHelper getRequest:funNameparameter:funpars];
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
我自己写了个 类 只不多是对 NSUrlConnection 和 对结果查询 进行了 封装 下载地址: http://download.youkuaiyun.com/detail/li6185377/4345010
里面 还附带了 对文件的上传 是别人写的 我就 合并了
因为 返回的数据 是Soap 数据 所以 我用GDataXml 对数据进行解析
如果 你们要直接用我的文件的化 记得加上GDATAXML
使用例子 :
WebServiceHelper* service = [[[WebServiceHelper alloc]initWebService:@"login"] autorelease];
[service addParameterForString:@"username" value:txt_username.text];
[service addParameterForString:@"password" value:txt_password.text];
service.delegate = self;
[service startASynchronous];
要记得 加上回调函数
-(void)requestFinished:(WebServiceHelper *)request
{
NSString* result = [request getSimpleResult];
NSArray *array = [result componentsSeparatedByString:@"{h}"];
NSString* code = [array objectAtIndex:0];
if(code.intValue == 1)
{
NSString* userinfo = [array objectAtIndex:1];
UserModel* model = [[UserModel alloc]initWithXmlData:userinfo];
[AppManager sharedManager].currentUserModel = model;
[model release];
[WToast showWithText:@"登陆成功!"];
[LoginController hideLoginController];
}
else if(code.intValue == -1)
{
[WToast showWithText:@"请核对账号密码后登陆。"];
}
else {
[WToast showWithText:@"系统繁忙!请稍后登陆。"];
}
}
-(void)requestFailed:(WebServiceHelper *)request
{
[WToast showWithText:@"系统繁忙!请稍后登陆。"];
NSLog(@"%@",request.error.debugDescription);
}
这是对应上传的 WebService 的方法
[WebMethod]
public string upload()
{
try
{
HttpFileCollection httpFiles = Context.Request.Files;
HttpPostedFile filePhoto = httpFiles["photo"];
string localPhotoFile = Server.MapPath(String.Format("imagefiles/{0}/{1}", "Food", filePhoto.FileName));
filePhoto.SaveAs(localPhotoFile);
NameValueCollection formVars = Context.Request.Form;
string Msg = formVars["message"];
File.WriteAllText("C:/sg.txt", Msg);
return "ok";
}
catch (System.Exception ex)
{
return "error " + ex.Message;
}
}