随笔-348  评论-598  文章-0  trackbacks-0
                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信息发送到网站。

---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2008-10-04 10:41 TiGERTiAN 阅读(1648) 评论(1)  编辑  收藏 所属分类: DotNetWindows Mobile

评论:
# re: C#获得和发送网站Session 2013-07-03 10:43 | crs
3Q,刚好有个本地保存sessionid的工作,参考一下,再次感谢!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: