小鬼之家

流浪,游走于文明与原始之间. 关注底层技术,实现美好生活。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
private static string PostData(string url, string postdata, CookieContainer cookieContainer)
        
{
            
string outdata = string.Empty;
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
            request.ContentType 
= "application/x-www-form-urlencoded";
            request.ContentLength 
= postdata.Length;
            request.UserAgent 
= userAgent;
            request.Method 
= "POST";
            request.CookieContainer 
= cookieContainer;
            
using (Stream inStream = request.GetRequestStream())
            
{
                
using (StreamWriter sw = new StreamWriter(inStream, Encoding.GetEncoding("gb2312")))
                
{
                    sw.Write(postdata);
                }

            }


            HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();
            response.Cookies 
= cookieContainer.GetCookies(request.RequestUri);
            
using (Stream outStream = response.GetResponseStream())
            
{
                
using (StreamReader sr = new StreamReader(outStream, Encoding.GetEncoding("gb2312")))
                
{
                    outdata 
= sr.ReadToEnd();
                }

            }


            
return outdata;
        }


        
private static string GetDate(string url, CookieContainer myCookieContainer)
        
{
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
            request.ContentType 
= "text/html";
            request.Method 
= "GET";
            request.CookieContainer 
= myCookieContainer;
            HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();

            
string outdata = string.Empty;

            
if (request.HaveResponse)
            
{
                
foreach (Cookie returnCookie in response.Cookies)
                
{
                    
bool cookieFound = false;

                    
foreach (Cookie oldCookie in myCookieContainer.GetCookies(request.RequestUri))
                    
{
                        
if (returnCookie.Name.Equals(oldCookie.Name))
                        
{
                            oldCookie.Value 
= returnCookie.Value;
                            cookieFound 
= true;
                        }

                    }


                    
if (!cookieFound)
                        myCookieContainer.Add(returnCookie);
                }

            }


            
using (Stream outStream = response.GetResponseStream())
            
{
                
using (StreamReader sr = new StreamReader(outStream, Encoding.GetEncoding("gb2312")))
                
{
                    outdata 
= sr.ReadToEnd();
                }

            }


            
return outdata;
        }
posted on 2007-06-07 23:55  黄尚  阅读(408)  评论(0编辑  收藏  举报