HttpWebRequest提交数据

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();

    }

 

你可能感兴趣的:(request)