ericyuan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
private string WebRequestMethod(string RequestUrl)
    {
        string str = "";
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        StreamReader reader = null;
        try
        {
            
            request = (HttpWebRequest) WebRequest.Create(RequestUrl);
            request.CookieContainer = this.Cookie;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
            response = (HttpWebResponse) request.GetResponse();
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            str = reader.ReadToEnd();
            reader.Close();
            response.Close();
        }
        catch (Exception exception)
        { 
            return "";
        }
        return str.Trim();
    }

    private string RequestPostMethod(string Post_Url, string PostData)
    {
        string str = "";
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        StreamReader reader = null;
        byte[] bytes = new ASCIIEncoding().GetBytes(PostData);
        try
        {
            request = (HttpWebRequest) WebRequest.Create(Post_Url);
            request.CookieContainer = this.Cookie;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Flush();
            requestStream.Close();
            response = (HttpWebResponse) request.GetResponse();
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            str = reader.ReadToEnd();
            reader.Close();
            response.Close();
        }
        catch (Exception exception)
        {
            return "";
        }
        return str.Trim();
    }

 

posted on 2013-08-07 12:25  ericyuan  阅读(253)  评论(0编辑  收藏  举报