数据采集

public static string PostWebRequest(string Data, string URL)
    {
        CookieContainer cc = new CookieContainer();
        string postData = Data;
        byte[] byteArray = Encoding.Default.GetBytes(postData); // 转化

        HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(URL));
        webRequest2.CookieContainer = cc;
        webRequest2.Method = "POST";
        webRequest2.ContentType = "application/x-www-form-urlencoded";
        webRequest2.ContentLength = byteArray.Length;
        Stream newStream = webRequest2.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
        newStream.Close();

        HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
        StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
        string text2 = sr2.ReadToEnd();
        if (text2 != null && text2.Length > 0)
        {
            return text2;
        }
        return "";
    }

    /// <summary>
    /// prosurfer------GET方式
    /// </summary>
    /// <param name="url">Url</param>
    /// <returns>PageHtml</returns>
    public static string GetPageHTML(string url)
    {
        WebRequest request = null;
        HttpWebResponse response = null;
        Stream stream = null;
        StreamReader sr = null;
        try
        {
            request = WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 2000;
            response = request.GetResponse() as HttpWebResponse;
            stream = response.GetResponseStream();
            sr = new StreamReader(stream, Encoding.Default);
            return sr.ReadToEnd();
        }
        catch
        {
            return string.Empty;
        }
        finally
        {
            if (sr != null)
            {
                sr.Close();
                sr.Dispose();
            }
            if (stream != null)
            {
                stream.Close();
                stream.Dispose();
            }
        }
    }

 

posted @ 2012-11-13 17:27  一千零一夜  阅读(201)  评论(0编辑  收藏  举报