request = (HttpWebRequest)WebRequest.Create(url);
if (Const.session != null)

{
request.Headers.Add("Cookie", Const.session);
}
request.Timeout = 30000; //設(shè)定超時值
request.ContentType = "application/octet-stream";
request.Method = "POST";
request.ContentLength = outData.Length;

stream = request.GetRequestStream();
stream.Write(outData, 0, outData.Length);
stream.Flush();
stream.Close();
Const.uiWaitMessage = "請求發(fā)送完畢,開始接收數(shù)據(jù)";
Thread.Sleep(500);
//發(fā)送完畢
//接收數(shù)據(jù)
response = (HttpWebResponse)request.GetResponse();
if (Const.session == null)

{// 注銷后要清除Common.session
String cookie = response.GetResponseHeader("Set-Cookie");
if (cookie != null)

{
int n = cookie.IndexOf(';');
if (n > -1) Const.session = cookie.Substring(0, n);
}
}
使用
if (Const.session == null)

{// 注銷后要清除Common.session
String cookie = response.GetResponseHeader("Set-Cookie");
if (cookie != null)

{
int n = cookie.IndexOf(';');
if (n > -1) Const.session = cookie.Substring(0, n);
}
}
可以獲得并保存網(wǎng)站分配的Session信息
這樣每次訪問指定url的時候就可以先用
if (Const.session != null)

{
request.Headers.Add("Cookie", Const.session);
}
來添加Cookie,將已獲得Session信息發(fā)送到網(wǎng)站。
---------------------------------------------------------
專注移動開發(fā)
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2008-10-04 10:41
TiGERTiAN 閱讀(1663)
評論(1) 編輯 收藏 所屬分類:
DotNet 、
Windows Mobile