2010-12-08 9 views
3

私は、GETとPOSTをRESTサービスに行うiPhoneクライアントを構築しています。 GETパーツは正常に動作しますが、POSTを実行できないようです。サーバーは現在のところxmlのみを受け入れます。 私はASIHTTPRequest、ASIFormDataRequestを使用しようとしましたが、それを自分でやっていますが、何も動かないようです。 誰でも助けてくれますか?私はObjective-Cのプログラミングをかなり新しくしています。 didReceiveResponseに次のコードを使用して応答として200:iPhoneからRESTサービスへのPOSTの使用に関する問題

私は、ステータスコードを取得行う

NSURL *url = [NSURL URLWithString:@"myUrl.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request appendPostData:[@"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]]; 
    // Default becomes POST when you use appendPostData:/appendPostDataFromFile:/setPostBody: 
    [request setRequestMethod:@"POST"]; 
    [request setDelegate:self]; 
    [request startSynchronous]; 
:ここ

NSHTTPURLResponse * httpResponse;  
httpResponse = (NSHTTPURLResponse *) response; 
int myStatus = httpResponse.statusCode; 
if (myStatus == 400) { 
    NSLog(@"Status code: 400"); 
} else if (myStatus == 200) { 
    NSLog(@"Status code: 200"); 
} else { 
    NSLog(@"Other status code"); 

は、3つの異なるアプローチ...

コード(ASIHTTPRequest)を以下の

コード(ASIFormDataRequest):

NSURL *url = [NSURL URLWithString:@"someUrl.com"]; 
    ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url]; 
//  [theRequest setPostValue:@"" forKey:@"key1"]; 
    [theRequest setPostValue:@"90" forKey:@"key2"]; 
    [theRequest setPostValue:@"150" forKey:@"key3"]; 
    // [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE]; 
    [theRequest startAsynchronous]; 

コード(第3法):

NSData *myPostData = [[NSString stringWithFormat:@"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding]; 
//  NSString *xmlPostData = @"<my xml>"; 
    NSURL *theURL = [NSURL URLWithString:@"someUrl.com"]; 

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"]; 
//  [theRequest setValue:@"UTF-8" forHTTPHeaderField:@"Charset"]; 
    [theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// myPostData]; 

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE]; 

どのようにこれらすべてについて - (無効)接続:スタイル機能?それらのいずれかが、接続が機能するための特定のコードを含んでいなければなりませんか?

何か助けてくれてありがとう!

+0

正確にはどのような問題がありますか? 200を受け取っている場合、サーバーは要求を受信して​​応答しました。サーバーまたはクライアントで動作が欠けていますか? –

+0

あなたの安らかなWebサービスで何か問題はありませんか? iPhoneのすべてが大丈夫だと思われるからです。 – alecnash

+1

私は実際に今すぐ働くようになった。 ASIHTTPRequestにContent-Typeが何を求めているかを具体的に伝えなければなりませんでした。あなた自身のコメントのためにとにかく感謝します!以下の作業コード。 – pbackvall

答えて

2
NSURL *url = [NSURL URLWithString:@"http://myURL.com"]; 

ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url]; 
NSData *myPostData = [[NSString stringWithFormat:@"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding]; 
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData]; 

[request addRequestHeader:@"Content-Type" value:@"application/xml"]; 
[request setPostBody:myMutablePostData]; 
[request setDelegate:self]; 
[request startSynchronous]; 
関連する問題