[英]How to set JSON NSData with ASIFormDataReuest
Hi I want to pass my userName and Password to WCF Webservice from iPhone. 嗨,我想将我的用户名和密码从iPhone传递到WCF Web服务。 I'm using ASIFormDataRequest.
我正在使用ASIFormDataRequest。
Here is my code 这是我的代码
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:txtUsername forKey:@"userName"];
[request setPostValue:txtPassword forKey:@"password"];
[request setDelegate:self];
[request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
[request startAsynchronous];
How do I pass JSON Data? 如何传递JSON数据? I could use this... [request appendPostBody:jsonData];
我可以使用这个... [requestPostPostBody:jsonData]; // JSON as NSData
// JSON作为NSData
but how do I set JSON to NSData? 但是如何将JSON设置为NSData?
NSData *jsonData = @"{'userName':'john', 'password':'secret'}"; NSData * jsonData = @“ {'userName':'john','password':'secret'}”;
Please help me! 请帮我!
Supposing you want to encode the JSON in UTF-8 as stated in your header: 假设您要按照标头中所述以UTF-8编码JSON:
NSString *json = @"{'userName':'john', 'password':'secret'}";
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostBody:jsonData];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.