2010-12-13 3 views

답변

3

는 나머지 프로토콜을 사용 입니다 :

두 가지 하위 유형 인 HTTP GET, HTTP POST가 있습니다. HTTP Get은 간단합니다.

속성에 값을 추가 할 수 있으며 직접 서비스를 호출 할 수 있습니다. 다음 코드를 확인하십시오.

NSString *url = @"http://123.456.789.0?action=get_movies"; 

여기서 get_movies 문자열을 서버에 action 속성으로 전달합니다. 을 입력 한 다음 서버에 요청하는 코드를 따르십시오.

NSURL *reqUrl = [[NSURL alloc] initWithString:url]; 
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:reqUrl]; 
NSError *error; 
NSURLResponse *response; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSStringEncoding responseEncoding = NSUTF8StringEncoding; 
if ([response textEncodingName]) { 
CFStringEncoding cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)[response textEncodingName]); 
if (cfStringEncoding != kCFStringEncodingInvalidId) { 
    responseEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding); 
    } 
} 
[reqUrl release]; 
NSString *dataString = [[NSString alloc] initWithData:data encoding:responseEncoding]; 

dataString은 서버의 responseString입니다. 당신은 그것을 사용할 수 있습니다.

감사합니다,

Satya.

+0

기기가 인터넷에 연결되어 있는지 확인하는 방법은 무엇입니까? – Piscean

5

나는 그것을 테스트 havent 한 경우에도이, 당신을 도울 수 있습니다

NSMutableString *httpBodyString; 
NSURL *url; 
NSMutableString *urlString; 

httpBodyString=[[NSMutableString alloc] initWithString:@"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace..."]; 
urlString=[[NSMutableString alloc] initWithString:@"http://www.somedomain.com/contactform.php"]; 

url=[[NSURL alloc] initWithString:urlString]; 
[urlString release]; 

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url]; 
[url release]; 

[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]]; 
[httpBodyString release]; 

NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 

if (!connectionResponse) 
{ 
    NSLog(@"Failed to submit request"); 
} 
else 
{ 
    NSLog(@"--------- Request submitted ---------"); 
    NSLog(@"connection: %@ method: %@, encoded body: %@, body: %a", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString); 
    NSLog(@"New connection retain count: %d", [connectionResponse retainCount]); 
    responseData=[[NSMutableData data] retain]; 
    NSLog(@"response", responseData); 
} 

출처 : 당신은 그 일에서, 이렇게 많은 방법으로 웹 서비스 할 수있는 문자열을 게시 할 수 있습니다 http://www.iphonedevsdk.com/forum/iphone-sdk-development/6341-help-executing-http-url-post-variables.html

+0

"POST"가 작동하지 않는 경우 (즉, 실제 방법이 아닌 경우) 위의 "PUT"을 시도해보십시오. – SK9

관련 문제