zoukankan      html  css  js  c++  java
  • 与采集有关的两个小函数

    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;
            }
  • 相关阅读:
    e.target和e.event和event.srcElement
    js代码优化
    史上最全的CSS hack方式一览
    学习NodeJS第一天:node.js引言
    响应式布局
    HTML+CSS编写规范
    英文SEO外部链接资源收集之常用的footprints
    判别木马
    php简单命令代码集锦
    zencart 新页面调用好功能代码集:
  • 原文地址:https://www.cnblogs.com/afxcn/p/775945.html
Copyright © 2011-2022 走看看