小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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

 昵稱10504424 2012-08-28

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

我們以登錄操作為例:

1、提交登錄數(shù)據(jù),并獲取Cookie

復(fù)制代碼
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("web服務(wù)地址");
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();
// 構(gòu)建SOAP內(nèi)容
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);
復(fù)制代碼


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

復(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();
// 構(gòu)建SOAP內(nèi)容
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();
復(fù)制代碼

 

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多