我正在使用以下内容将电子邮件和密码发布到我的服务器(php脚本)。我遇到的问题是密码包含一个特殊字符(特别是&符号),该字符似乎被剥夺了。我猜想是因为它认为要传递其分离变量。如何在不删除它的情况下传递此char?
let myURL = NSURL(string: "my script url here")
let request = NSMutableURLRequest(URL: myURL!)
request.HTTPMethod = "POST"
let postString = "email=\(userEmailText)&password=\(userPasswordText)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用消息正文中的某些参数构造一个POST请求,但我正在努力处理/查看响应.
这是我试图在Obj C中重新创建的请求:
我对创建此请求的代码有些熟悉,但我正在努力将响应解析为有意义的数据.这是我的方法目前看起来像这样(它似乎是成功的):
NSLog(@"WEB SERVICE CALLED!");
NSURL *aUrl = [NSURL URLWithString:@"xxx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"grant_type=password&username=xxx&password=xxx";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if(connection) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能看到我的回答是什么?在此先感谢您的帮助!
UPDATE
我添加了这个位,可以看到我的响应给了我一个代码200(成功),但数据对象看起来需要序列化,因为它看起来像一个十六进制字符串.这是我正在做的处理响应/数据/错误:
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// your data or an error will be ready here
NSLog(@"RESPONSE: %@",response);
NSLog(@"DATA: %@",data);
NSLog(@"ERROR: %@",error);
}];
Run Code Online (Sandbox Code Playgroud)