2012-09-09 44 views
1

我正在開發一個Windows Phone應用程序。它需要對站點進行驗證。我將userid和密碼發送給WCF服務,該服務將檢查身份驗證。Wcf服務不返回餅乾

服務端代碼:

Service.svc.cs

public CookieContainer GetConnect(string uid, string password) 
//public string GetConnect(string uid, string password) 
{ 
    try 
    { 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://example.com/Login1.action"); 
     req.Method = "POST"; 
     CookieContainer con = new CookieContainer(); 
     req.CookieContainer = con; 
     req.CookieContainer.Add(GetCookies()); 
     req.KeepAlive = true; 

     req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 

     req.Referer = "http://example.com/content/index.html"; 

     byte[] data = Encoding.Default.GetBytes("username=" + uid + "&password=" + password); 
     req.Credentials = new NetworkCredential(uid, password); 
     req.ContentLength = data.Length; 
     req.AllowAutoRedirect = true; 
     req.ServicePoint.Expect100Continue = true; 

     string str = req.GetRequestStream(); 
     str.Write(data, 0, data.Length); 
     str.Close(); 

     HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 

     string iduri = System.Web.HttpUtility.ParseQueryString(res.ResponseUri.Query).Get("id"); 
     if (iduri != "") 
     { 
      return con; 
      //return "Success"; 
     } 
     else 
     { 
      res.Close(); 
      str.Close(); 

      return null; 
      //return "Fail"; 
     } 
    } 
    catch (Exception) 
    { 
     return null; 
    } 
} 

IService.cs

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    CookieCollection GetCookies(); 

    [OperationContract] 
    CookieContainer GetConnect(string uname, string password); 
    //string GetConnect(string uname, string password); 
} 

客戶端代碼:

個Login.xaml.cs

void svc_Get_Connected(object send, GetConnectCompletedEventArgs e) 
{ 
    var cc = new CookieContainer(); 
    cc=e.Result; 
} 

當我retun CON對象時,它給以下錯誤:

There was no endpoint listening at http://example.com:3922/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."

如何返回CookieContainer

+0

你的問題有點難以閱讀 - 如果你在格式化工作,刪除不相關的(評論)代碼,它會有所幫助。請注意,您可以隨時編輯您的問題。無論哪種方式,我都不清楚整個cookie與問題有什麼關係:您發佈的錯誤表明您的端點配置存在問題? – Jeroen

+0

好吧,看編輯問題 –

+0

所以你想從WCF服務方法返回'CookieContainer'?您只能返回由'DataContract'和'DataMember'屬性裝飾的類。 – abatishchev

回答

0

你需要來裝飾你的返回對象類,以便WCF能夠序列化,例如:

[DataContract] 
public class CookieContainer 
{ 
    [DataMember] 
    public string SomeProperty { get; set; } 
    [DataMember] 
    public string SomeOtherProperty { get; set; } 
    // you get the idea... 
} 

不要忘記建立自己的服務,運行它,然後要麼刷新客戶端服務引用或手動重新生成客戶端代理代碼&配置。