2010-12-08 3 views
3

GET 및 POST를 REST 서비스로 할 iPhone 클라이언트를 빌드하고 있습니다. GET 부분은 정상적으로 작동하지만 POST를 수행 할 수있는 것 같지 않습니다. 서버는 현재 xml만을 허용합니다. 나는 ASIHTTPRequest, ASIFormDataRequest를 사용하려고 시도했지만 나 자신을하고 있지만 아무것도 작동하지 않는 것 같다. 아무도 도와 줄 수 있습니까? 나는 Objective-C 프로그래밍에 대해 상당히 새로운 것이다. didReceiveResponse에 다음 코드를 사용하여 응답 200 :iPhone에서 POST로 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"); 

는 다음 세 가지 방법 ...

코드 (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

편안한 웹 서비스로 모든 것이 정상입니까? 아이폰에있는 모든 것들이 좋아 보인다. – alecnash

+1

나는 실제로 지금 일하도록했다. ASIHTTPRequest에게 내가 원하는 Content-Type을 구체적으로 말해야했습니다. 그것이 그 자체를 위해 밖으로 1를 분류 할 것이다라고하는 생각 그러나 no .... 어떻게해서든지 당신의 설명 사람을위한 감사! 아래 작업 코드. – 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]; 
관련 문제