简单的POST提交方法

3 篇文章 0 订阅

 

public static string Login(String url, String paramList)
    {

        HttpWebResponse res = null;

        string strResult = "";

        try
        {

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Method = "POST";

            req.ContentType = "application/x-www-form-urlencoded";

            req.AllowAutoRedirect = false;

            CookieContainer cookieCon = new CookieContainer();

            req.CookieContainer = cookieCon;

            StringBuilder UrlEncoded = new StringBuilder();

            Char[] reserved = { '?', '=', '&' };

            byte[] SomeBytes = null;

            if (paramList != null)
            {

                int i = 0, j;

                while (i < paramList.Length)
                {

                    j = paramList.IndexOfAny(reserved, i);

                    if (j == -1)
                    {

                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));

                        break;

                    }

                    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));

                    UrlEncoded.Append(paramList.Substring(j, 1));

                    i = j + 1;

                }

                SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());

                req.ContentLength = SomeBytes.Length;

                Stream newStream = req.GetRequestStream();

                newStream.Write(SomeBytes, 0, SomeBytes.Length);

                newStream.Close();
            }
            else
            {

                req.ContentLength = 0;

            }
            res = (HttpWebResponse)req.GetResponse();

            Stream ReceiveStream = res.GetResponseStream();

            Encoding encode = System.Text.Encoding.GetEncoding("GBK");

            StreamReader sr = new StreamReader(ReceiveStream, encode);

            Char[] read = new Char[256];

            int count = sr.Read(read, 0, 256);

            while (count > 0)
            {

                String str = new String(read, 0, count);

                strResult += str;

                count = sr.Read(read, 0, 256);

            }

        }
        catch (Exception e)
        {
            strResult = e.ToString();
        }
        finally
        {
            if (res != null)
            {
                res.Close();

            }
        }
        return strResult;

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值