使用HttpWebRequest访问Web服务,并传递Cookie数据

有时候难免会在项目中使用到web服务,可以利用vs生成web服务访问代理。不过呢,我们在这儿使用HttpWebReqeust来访问web服务,并访问Cookie

我们以登录操作为例(Soap 1.1):

1、提交登录数据,并获取Cookie

System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("web服务地址");
req.Method = "POST";
//req.ContentType = "application/x-www-form-urlencoded"
req.ContentType = "text/xml; charset=utf-8";
//此处限定web服务方法
req.Headers.Add(
"SOAPAction", "\"http://www.demo.com/Login\""); //这儿必须添加CookieContainer,不然后面得不到Cookie
req.CookieContainer
= new System.Net.CookieContainer(); //准备构建Soap内容
StringBuilder soap
= new StringBuilder(); // 构建SOAP内容
soap.AppendLine(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"); soap.AppendLine("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"); soap.AppendLine(" <soap:Body>"); soap.AppendLine(" <Login xmlns=\"http://www.demo.com/\">"); soap.AppendLine(" <username>用户名</username>"); soap.AppendLine(" <password>密码</password>"); soap.AppendLine(" </Login>"); soap.AppendLine(" </soap:Body>"); soap.AppendLine("</soap:Envelope>"); //获取请求的流对象,以方便写入soap内容 System.IO.StreamWriter reqStream = new System.IO.StreamWriter(req.GetRequestStream()); reqStream.Write(soap.ToString()); reqStream.Close();
//获取返回的流对象
System.Net.HttpWebResponse rep
= req.GetResponse(); System.IO.StreamReader reader = new System.IO.StreamReader(rep.GetResponseStream()); //输出返回的数据
TextBox1.Text
= reader.ReadToEnd(); reader.Close(); rep.Close(); //获取Cookie
System.Net.Cookie cookie
= rep.Cookies("cookie名称"); if(cookie != null){
Response.Write(cookie.Value);
}


2、获取登录用户信息(会话访问),将Cookie发送回服务器端

System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("http://www.demo.com/webtools/webservice/web/youjuhuiservice.asmx");
req.Method = "POST";
req.ContentType = "text/xml; charset=utf-8";
//请求的web服务方法
req.Headers.Add(
"SOAPAction", "\"http://www.demo.com/GetOnlineUser\""); //添加CookieContainer,不然无法传递Cookie数据
req.CookieContainer
= new System.Net.CookieContainer(); //创建一个Cookie,此Cookie不是HttpCookie,但结构差不多的
System.Net.Cookie cookie
= new System.Net.Cookie("cookie名称", "cookie值"); //设置Cookie作用域
cookie.Domain
= www.demo.com; //将Cookie添加到请求中
req.CookieContainer.Add(cookie); StringBuilder soap
= new StringBuilder(); // 构建SOAP内容
soap.AppendLine(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"); soap.AppendLine("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"); soap.AppendLine(" <soap:Body>"); soap.AppendLine(" <GetOnlineUser xmlns=\"http://www.demo.com/\" />"); soap.AppendLine(" </soap:Body>"); soap.AppendLine("</soap:Envelope>"); //获取请求的流对象,以方便写入soap内容
System.IO.StreamWriter reqStream
= new System.IO.StreamWriter(req.GetRequestStream()); reqStream.Write(soap.ToString()); reqStream.Close(); //获取响应的流对象,以分析获取内容
System.Net.HttpWebResponse rep
= req.GetResponse(); System.IO.StreamReader reader = new System.IO.StreamReader(rep.GetResponseStream()); //输出内容
TextBox1.Text
= reader.ReadToEnd(); reader.Close(); rep.Close();

 

 

posted @ 2012-08-24 14:47  追梦客2008  阅读(991)  评论(0编辑  收藏  举报