• C#POST数据


    第一种方法:

    #region 同步通过POST方式发送数据
            /// <summary>
            /// 通过POST方式发送数据
            /// </summary>
            /// <param name="Url">url</param>
            /// <param name="postDataStr">Post数据</param>
            /// <param name="cookie">Cookie容器</param>
            /// <returns></returns>
            public string SendDataByPost(string Url,string postDataStr,ref CookieContainer cookie)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                if (cookie.Count == 0)
                {
                    request.CookieContainer = new CookieContainer();
                    cookie = request.CookieContainer;
                }
                else
                {
                    request.CookieContainer = cookie;
                }

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postDataStr.Length;
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString= myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;
            }
            #endregion

            #region 同步通过GET方式发送数据
            /// <summary>
            /// 通过GET方式发送数据
            /// </summary>
            /// <param name="Url">url</param>
            /// <param name="postDataStr">GET数据</param>
            /// <param name="cookie">GET容器</param>
            /// <returns></returns>
            public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url +( postDataStr == "" ? "" : "?") + postDataStr);
                if (cookie.Count == 0)
                {
                    request.CookieContainer = new CookieContainer();
                    cookie = request.CookieContainer;
                }
                else
                {
                    request.CookieContainer = cookie;
                }

                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
               
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;
            }
            #endregion

    第二种方法:

        public static string PostData(string data,string url)
        {
            WebClient wc = new WebClient();
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = Encoding.ASCII.GetBytes(data);
            byte[] responseData = wc.UploadData(url, "POST", postData);
            return Encoding.UTF8.GetString(responseData);
        }

  • 相关阅读:
    c++小游戏——2048
    c++小游戏——贪吃蛇
    218 事件处理 on() 绑定事件
    217 jQuery 元素操作:遍历,创建,添加(append、prepend、after、before),删除(remove、empty、html)
    216 jQuery 文本属性值:html() 、text() 、 val()
    215 jQuery 属性操作:prop() 、 attr() 、 data()
    214 jQuery案例:王者荣耀手风琴效果
    213 jQuery 事件切换:hover()
    212 停止动画排队: stop()
    211 jQuery 自定义动画:animate()
  • 原文地址:https://www.cnblogs.com/booth/p/2125906.html
Copyright © 2020-2023  润新知