wcf xml消息客户端cookie

时间:2022-08-11 20:05:01
public override bool Login(string userName, string password)
{ using (HttpClient client = new HttpClient(URI))
{
StringBuilder sb = new StringBuilder();
sb.Append("Login"); Uri uri = new Uri(URI + sb.ToString());
Login lg = new Login();
lg.userName = userName;
lg.password = password;
HttpContent content = HttpContentExtensions.CreateXmlSerializable<Login>(lg, null); StringBuilder conntentStr = new StringBuilder();
//string s = content.ReadAsString();
using (HttpRequestMessage request = new HttpRequestMessage("POST", uri, content))
{
request.Headers.Accept.AddString(accepts);
using (HttpResponseMessage response = client.Send(request))
{
Microsoft.Http.Headers.Cookie cook=null;
if(response.Headers.SetCookie!=null && response.Headers.SetCookie.Count>)
{
cook= response.Headers.SetCookie[];
CookieMe.SaveCookie(cook["YourAuth"]);
}
Stream result = response.Content.ReadAsStream();
XmlSerializer ser = new XmlSerializer(typeof(LoginResponse));//将对象序列化到xml文档
LoginResponse lr = (LoginResponse)ser.Deserialize(result);
return lr.LoginResult;
}
} }
}

使用wcf starter kit,注意协定操作方法名和参数的序列化是一起的,因为服务端使用了

BodyStyle = WebMessageBodyStyle.Wrapped

注意序列化时的对象名必须与协定中的方法参数名大小写一致,否则会出现错误

登陆后客户端需要保存cookie,远程调用其他操作都要先向HttpRequestMessage对象添加cookie

request.Headers.Cookie.Add(cookie);