2012-09-09 109 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... 
} 

不要忘记建立自己的服务,运行它,然后要么刷新客户端服务引用或手动重新生成客户端代理代码&配置。