我有WCF服务,我想从我的Web服务中获取数据.但是URL总是返回nil.为什么?我想向来自文本字段值的WCF发送参数.例如;
I have WCF service and I want to get data from my web service. But URL always returns nil. Why? I want to send parameter to WCF which comes from text field value. For example;
txtfield.text = @"ATAŞEHİR";怎么了?
NSString *request = [NSString stringWithFormat:@"www.xxxxxxxx/CCWCF.svc/MethodName/%@",txtfield.text]; NSURL *URL = [NSURL URLWithString:[request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];我尝试了不同的方法,但结果没有改变.
I tried different things but result didn't change.
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"www.xxxxxxxx/CCWCF.svc/MethodName/%@",txtfield.text]];或:
NSURL *URL = [[NSURL alloc] initWithString:[request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 推荐答案NSString* encodedText = [txtfield.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *request = [NSString stringWithFormat:@"www.xxxxxxxx/CCWCF.svc/MethodName/%@",encodedText]; NSURL *URL = [NSURL URLWithString:request];
另外, [NSString stringByAddingPercentEscapesUsingEncoding:] 对于URL编码可能会有些问题.使用 Core Foundation 有一种更安全的方法.参见 madebymany/blog/url-encoding-an-例如nsstring-on-ios (找不到SO问题).
On a separate note, [NSString stringByAddingPercentEscapesUsingEncoding:] can be a bit problematic for URL encoding. There is a safer way using Core Foundation. See madebymany/blog/url-encoding-an-nsstring-on-ios, for example (can't find the SO question).
更多推荐
NSURL总是返回Nil
发布评论