[英]Wcf service not returning cookies
我正在开发一个Windows Phone应用程序。 它要求对站点进行身份验证。 我将用户名和密码发送到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;
}
当我重新调整con对象时,出现以下错误:
在http://example.com:3922/Service1.svc上没有侦听终结点的端点可以接受该消息。 这通常是由不正确的地址或SOAP操作引起的。 有关更多详细信息,请参见InnerException(如果存在)。”
如何返回CookieContainer
?
您需要装饰返回对象类,以便WCF可以对其进行序列化,例如:
[DataContract]
public class CookieContainer
{
[DataMember]
public string SomeProperty { get; set; }
[DataMember]
public string SomeOtherProperty { get; set; }
// you get the idea...
}
不要忘记构建服务,运行它,然后刷新客户端服务参考或手动重新生成客户端代理代码和配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.