日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

使用HttpWebRequest訪問Web服務,并傳遞Cookie數(shù)據(jù)

 昵稱10504424 2012-08-28

有時候難免會在項目中使用到web服務,可以利用vs生成web服務訪問代理。不過呢,我們在這兒使用HttpWebReqeust來訪問web服務,并訪問Cookie

我們以登錄操作為例:

1、提交登錄數(shù)據(jù),并獲取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";
req.Headers.Add("SOAPAction", "\"http://www./Login\"");
req.CookieContainer = new System.Net.CookieContainer();
StringBuilder soap = new StringBuilder();
// 構建SOAP內容
soap.AppendLine(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"); soap.AppendLine("<soap:Envelope xmlns:xsi=\"http://www./2001/XMLSchema-instance\" xmlns:xsd=\"http://www./2001/XMLSchema\" xmlns:soap=\"http://schemas./soap/envelope/\">"); soap.AppendLine(" <soap:Body>"); soap.AppendLine(" <Login xmlns=\"http://www./\">"); soap.AppendLine(" <username>用戶名</username>"); soap.AppendLine(" <password>密碼</password>"); soap.AppendLine(" </Login>"); soap.AppendLine(" </soap:Body>"); soap.AppendLine("</soap:Envelope>"); 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()); //輸出返回的數(shù)據(jù)
TextBox1.Text
= reader.ReadToEnd(); reader.Close(); rep.Close(); //獲取Cookie
System.Net.Cookie cookie
= rep.Cookies("cookie名稱");
Response.Write(cookie.Value);
復制代碼


2、獲取登錄用戶信息(會話訪問),將Cookie發(fā)送回服務器端

復制代碼
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("http://www./webtools/webservice/web/youjuhuiservice.asmx");
req.Method = "POST";
req.ContentType = "text/xml; charset=utf-8";
req.Headers.Add("SOAPAction", "\"http://www./GetOnlineUser\"");
req.CookieContainer = new System.Net.CookieContainer();
System.Net.Cookie cookie = new System.Net.Cookie("cookie名稱", "cookie值");
cookie.Domain = "www.";
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./2001/XMLSchema-instance\" xmlns:xsd=\"http://www./2001/XMLSchema\" xmlns:soap=\"http://schemas./soap/envelope/\">"); soap.AppendLine(" <soap:Body>"); soap.AppendLine(" <GetOnlineUser xmlns=\"http://www./\" />"); soap.AppendLine(" </soap:Body>"); soap.AppendLine("</soap:Envelope>"); 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();
復制代碼

 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多