我正在开发一个 Windows Phone 应用程序。它需要对站点进行身份验证。我将用户名和密码发送到 WCF 服务,该服务将检查身份验证。
服务端代码:
服务.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);
}
客户端代码:
登录.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
?