iOS デバイスから WCF サーバへ POST でパラメータ (JSON形式) を送信する方法

– ASIFormDataRequest の場合

ASIFormDataRequest クラスを使用して POST する場合、Content-Type は以下のものが指定される。
‘application/x-www-form-urlencoded’
‘multipart/form-data’

上記以外の Content-Tpye を明示的に指定し、POST する場合は以下の様にする。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Http通信 postでパラメータ(json形式)をサーバへ渡す
NSString *interfaceURL;
interfaceURL = [NSString stringWithFormat:@"http://(サーバ アドレス)"];    
 
NSURL *url = [NSURL URLWithString:interfaceURL];
request_ = [[ASIFormDataRequest alloc] initWithURL:url];
request_.delegate = self;
[request_ addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
 
// WCFインタフェースへ渡すパラメータを json 形式に変換する
// NSDictionary → (json文字列) NSString
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:@"(値1)", @"(キー1)",  
                                                  @"(値2)", @"(キー2)", 
                                                  nil];
 
SBJsonWriter *writer = [[[SBJsonWriter alloc] init] autorelease];
writer.humanReadable = YES;
writer.sortKeys = YES;
NSLog(@"送信パラメータ:%@", [writer stringWithObject:dict]);
 
NSString *postParameterString;
postParameterString = [NSString stringWithFormat:@"%@", [writer stringWithObject:dict]];
 
// パラメータをボディにセットし、WCF サーバへ通信する
[request_ appendPostData:[postParameterString dataUsingEncoding:NSUTF8StringEncoding]];    
[request_ startAsynchronous];

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です