'Encode'에 해당되는 글 1건

  1. 2011.07.22 [iphone] WCF Rest Service로 HttpRequest하는법과 한글 인코딩
iPhone App2011. 7. 22. 18:51

일단 내가 하고 싶었던것은 GET방식의 URL을 HttpRequest 를 해서 WCF에 전송하게 한뒤 글을 쓰게 만드는것이 목표였다.

일단 헤더에 NSMutableData를 만들고

-(void) save{

responseData = [NSMutableData new];

NSString *serviceURL = [NSString stringWithFormat:@"http://192.168.10.3:9090/hugeboardservice/write?name=%@&title=%@&description=%@",

[self encodeString:nameTextField.text],

[self encodeString:titleTextField.text],

[self encodeString:descriptionTextView.text]];


NSURL *url = [NSURL URLWithString:serviceURL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

  

[request setHTTPMethod:@"GET"];

  

[[NSURLConnection alloc] initWithRequest:request delegate:self];

[self.navigationController popViewControllerAnimated:YES];

}


이렇게 했다 저기 위에 보면 NSURLConnectino을 delegate 받았다.


그래서 추가적으로 4개의 델리게이트 메소드를 만들었다.


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

[responseData setLength:0];

}


- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[responseData appendData:data];

}


- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

[connection release];

  

NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"the html from google was %@", responseString);

  

[responseString release];

}


-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

NSLog(@"something very bad happened here");

}


참고한곳은 http://toranbillups.com/blog/archive/2011/04/10/Writing-your-first-http-request-in-objective-c


여기이다.

하지만 이렇게 하다보니 문제가 생겼다 한글을 파라미터로 하면 오류가 났던것이다. 아마 인코딩을 안해서 그렇다고 생각해서


//한글떄문에 엔코딩하기 위해서 만든 메소드

-(NSString *)encodeString: (NSString*) unencodedString{

NSString *temp = (NSString *)CFURLCreateStringByAddingPercentEscapes(

NULL,

(CFStringRef)unencodedString,

NULL,

(CFStringRef)@"!*'();:@&=+$,/?%#[]",

kCFStringEncodingUTF8 );

  

return temp;

}


이런 메소드를 추가로 만들었다
만드는데 참고한 사이트는
http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/


***추가***

save를 하고 뒤로 가기 위해서 save메소드에 

[self.navigationController popViewControllerAnimated:YES];

이걸 넣었는데

여기에 넣는게 아니라 

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; [self.navigationController popViewControllerAnimated:YES]; }

이쪽에 넣는것이 맞다. 한마디로 작업이 완료가 되면 뒤로 넘어가야지 바로 넘어가면 안되는것이다.

참고 : http://www.pcraft.kr/101


Posted by 동동(이재동)