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 동동(이재동)
wcf2011. 5. 17. 15:57

자 Rest Service를 만들어 보자

 

RestService의 최대강점은  멀티 플랫폼할때 좋다는거다..

 

인자도 그냥 URL형식으로 Get,Post 방법으로 보내면 되고 그렇게 되면 언어나 플랫폼에 상관없이

 

쓸수 있는 아주 편한 진정한 RIA 서비스가 된다.

 

첫번째

 

new-project 해서 새로운 프로젝트를 만들고

 

Online Templates로 간다.. 아마 비쥬얼 스튜디오 2010을 깔았다면 그냥 wcf service가 있을것이다.

 

하지만 우리는 WCF REST Service를 만들것이기때문에 online Template으로 가서 프로젝트를 만든다.

 

image

 

만든뒤에

 

Service1.cs가 보일것이다.

 

물론  이걸써도 돼지만 새롭게 만드는것도 나쁘지 않다.

 

나는 윈폰에서 쓸 Notification Server를 만들꺼기때문에 Notifications.cs란 class를 새로 만들었다.

 

[ServiceContract]
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
   public class Notifications
   {
       private static List<Uri> subscribers = new List<Uri>();
       private static object obj = new object();
 
       [WebInvoke(UriTemplate = "register?uri={uri}", ResponseFormat = WebMessageFormat.Json, Method = "GET")]
       public void Register(string uri)
       {
           // TODO: Return the instance of SampleItem with the given id
           Uri channelUri = new Uri(uri, UriKind.Absolute);
           Subscribe(channelUri);
       }

 

그리고 클래스위에 ServiceContract를 정의 하였으며

 

저렇게 메소드를 만들어서 인자를 받게 만들었다..

 

그리고 중요한 Global.aspx.cs에서

 

 

private void RegisterRoutes()
{
    // Edit the base address of Service1 by replacing the "Service1" string below
    RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
    RouteTable.Routes.Add(new ServiceRoute("Notifications", new WebServiceHostFactory(), typeof(Notifications)));
}

 

 

이렇게 추가하고 컴파일한후

 

http://localhost:19976/notifications

 

컴파일 뒤에 notifications를 붙여서 접속하면 접속이 잘된다.

 

이제 머 메소드를 붙여 넣든 마음대로 요리하면 된다.

 

아참 이 Registor 메소드를 부르는쪽에서는 이렇게 호출한다.

 

/// <summary>
/// MS에서 받아온 url을 서버(WCF Service)에 보낸다.
/// </summary>
private void SubscribeToService()
{
    string baseUri = "http://localhost:19976/Notifications/Register?uri={0}";        
 
    string theUri = String.Format(baseUri, httpChannel.ChannelUri.ToString());
 
    WebClient client = new WebClient();
    client.DownloadStringCompleted += (s, e) =>
        {
             if (e.Error == null)
             {
                 Dispatcher.BeginInvoke(() => UpdateStatus("Registration Success"));
             }
             else 
             {
                 Dispatcher.BeginInvoke(() => UpdateStatus(e.Error.Message));
             }
 
        };
    client.DownloadStringAsync(new Uri(theUri));
}

 

 

쉽다.

Posted by 동동(이재동)