request = (HttpWebRequest)WebRequest.Create(url);
if (Const.session != null)
{
request.Headers.Add("Cookie", Const.session);
}
request.Timeout = 30000; //设定超时值
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 = "请求发送完毕,开始接收数据";
Thread.Sleep(500);
//发送完毕
//接收数据
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);
}
}
可以获得并保存网站分配的Session信息
这样每次访问指定url的时候就可以先用
if (Const.session != null)
{
request.Headers.Add("Cookie", Const.session);
}
来添加Cookie,将已获得Session信息发送到网站。
C#获得和发送网站Session