cross domain 해결하기

-- ASP.NET 2012. 3. 19. 14:45
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

cross domain에 대해 생각해보다가 아래의 사이트의 개념이 이뻐서 정리해본다.
핵심은 AJAX를 이용해 해당 정보를 HTML로 가져오는 것이다. (Good ;) )

 

[HttpGet]

public string MyStudyList() 
{        
    
string result = String.Empty;
    
HttpWebRequest request = WebRequest.Create(String.Format("yoursite{0}",Request.Url.Query)) as HttpWebRequest;
    request.Method = 
"GET";
    request.ContentType = 
"application/x-www-form-urlencoded";
    request.CookieContainer = 
new CookieContainer();
    
HttpCookieCollection cookies = Request.Cookies;

    
for (int i = 0; i < cookies.Count; i++)
    {
        
HttpCookie httpCookie = cookies.Get(i);
        
Cookie cookie = new Cookie();
        cookie.Domain = request.RequestUri.Host;
        cookie.Expires = httpCookie.Expires;
        cookie.Name = httpCookie.Name;
        cookie.Path = httpCookie.Path;
        cookie.Secure = httpCookie.Secure;
        cookie.Value = httpCookie.Value;
        request.CookieContainer.Add(cookie);
    }        
        
    
WebResponse response = request.GetResponse();
    
using (Stream stream = response.GetResponseStream())
    {
        
using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(51949)))
        {                
            result = sr.ReadToEnd();
        }
    }
        
    
return result;
}


위의 코드에서 중요한 부분은 for로 쌓여 있는 부분이다.
해당 URL에는 쿠키 기반의 인증 절차로 로그인한 자의 쿠키값을 가지고 어떠한 행위를 한다.
위의 구문과 같이 쿠리를 컨테이너에 담에 Request를 보낸다.

출처 : http://it-developer.tistory.com/199

posted by 어린왕자악꿍