2

NSUrlConnection を使用して、iOS アプリケーションで安らかな Web サービスを試みています。以下のコードを使用して json 文字列をフォーマットし、安らかな Web サービスに送信しています。しかし、connectionReceiveResponse: メソッドで '415(unsupported media type)' 応答を取得しています。ここでどこが間違っているのか教えてください。

NSURL *urlLoginAuthentication= [NSURL URLWithString:@" http://www.loginService/mp/api "];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlLoginAuthentication];
[request setHTTPMethod:@"POST"];

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:self.txtUserName.text, self.txtPassWord.text,nil] forKeys:[NSArray arrayWithObjects:@"username",@"password", nil]];

NSLog(@"Dict: %@",jsonDict);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString);

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"contentType"];

[request setValue:@"json" forHTTPHeaderField:@"dataType"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSLog(@"jsonData: %@",jsonData);

self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
4

1 に答える 1

7

リクエストで送信した Content-Type がサポート タイプでない場合、HTTP ステータス コード 415 を受け取ります。コードを見ると、Content-Type ヘッダーの設定が間違っています。この行:

[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"contentType"];

する必要があります

[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
于 2013-10-10T17:50:51.493 に答える