Windows Phone 72011. 11. 11. 15:27

아… 대단하다 이것 때문에 하루종일 샤워 할때도.. 꿈꿀때도 아침에 일어날때도 계속 이것을 어떻게 해결해야하나 싶었다.

현재 윈폰용 코레일 앱을 만드는데 EUC-KR에 이어 난항을 겪었다.

사건의 발단은 이렇다.

코레일에서 Rest Sevice를 이용해서 XML등을 읽어와서 파싱하는데

예약 현황 같은것은 로그인을 해야만 가능한것이였다.

웹에서를 예로 들면

로그인 인증 페이지 URL을 접속 해야만 예약 현황을 볼수 있는 페이지를 볼수 있었다.

만약 로그인 인증 페이지 URL을 먼저 접속하지 않으면 예약 현황 페이지에서는 로그인을 해야만 볼수 있다라고 뜬다.

그래서 내가 한방법은 Webclient를 이용해서 로그인 인증 페이지를 먼저 접속 후 예약 현황 페이지를 다시 호출한것이였다.

하지만 그 방법 역시 작동을 안했다. 구글링을 해서 좀더 로우레벨로 해보기로 하여 HttpWebRequest로도 해보고 아이폰쪽 소스도 보고 별짓을 다 했다.

결국 해결한것은 바로 쿠키였다 철도청 관계자는 서버 세션 유지라고 해서 난 쿠키랑 아무런 관계가 없을줄 알았다. 여기서 시간을 엄청 소비했다.

태규형님에게 도움을 요청했고 비슷한 사례를 하나 찾아주셧다.

http://bytes.com/topic/net/answers/426631-web-service-session-winform-client

결국난 CookieContainer 를 설정해야 한다는것을 깨닫고

Webclient를 override해서 가능하다는것을 깨달았다.

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/149d545f-b605-4fce-b29a-2bfb1c51fbbe

하지만 그것은 WinForm에서는 작동했지만 윈폰에서는 약간의 보안문제로 다시 수정,보완을 하였다.

결국 성공.. 이틀동안의 삽질에 대한 눈물이 났다. ㅠㅠ

다음은 이틀 동안 고생해서 내가 만든 윈폰7용 소스이다. 이건 진짜 인터넷에 하나도 없다 ㅠㅠ

public class CookieAwareWebClient : WebClient
    {
        private CookieContainer m_container = new CookieContainer();

        [SecuritySafeCritical]
        public CookieAwareWebClient()
        {
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = m_container;
            }
            return request;
        }
    }

사용은 그냥

 _webClient.OpenReadAsync(new Uri(url));  _webClient.OpenReadCompleted 
 
를 사용하였다.             
Posted by 동동(이재동)
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 동동(이재동)