开发者

How to set JSON NSData with ASIFormDataReuest

开发者 https://www.devze.com 2023-04-01 03:33 出处:网络
Hi I want to pass my use开发者_运维问答rName and Password to WCF Webservice from iPhone. I\'m using ASIFormDataRequest.

Hi I want to pass my use开发者_运维问答rName and Password to WCF Webservice from iPhone. I'm using 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? I could use this... [request appendPostBody:jsonData]; // JSON as NSData

but how do I set JSON to NSData?

NSData *jsonData = @"{'userName':'john', 'password':'secret'}";

Please help me!


Supposing you want to encode the JSON in UTF-8 as stated in your header:

NSString *json = @"{'userName':'john', 'password':'secret'}";
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostBody:jsonData];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号