• C# get post 的方法


    #region GET POST
    
    
            /// <summary>
            /// Get String data = GetString(URL , "PKEY=" + Pkeylist);
            /// </summary>
            /// <param name="URL"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static string GetString(string URL, string parameters)
            {
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = Encoding.UTF8;
                // 准备请求...
                try
                {
                    // 设置参数
                    request = WebRequest.Create(URL +"?"+ parameters) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "GET";
                    request.ContentType = "application/x-www-form-urlencoded";
    
    
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content.Trim();
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    return err;
                }
            }
            /// <summary>
            /// POST string data = PostString(URL , "PKEY=" + Pkeylist);
            /// </summary>
            /// <param name="URL"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static string PostString(string URL, string parameters)
            {
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = Encoding.UTF8;
                byte[] data = encoding.GetBytes(parameters);
                // 准备请求...
                try
                {
                    // 设置参数
                    request = WebRequest.Create(URL) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content.Trim();
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    return err;
                }
            }
    
    
     #endregion 
  • 相关阅读:
    POJ 1265 Pcik定理
    POJ 1380 坐标旋转
    POJ 1788
    POJ 3714 平面最近点对
    POJ 1905 二分
    POJ 1151 矩形面积并
    POJ 1654 多边形面积
    ZOJ 1010 判断简单多边形+求面积
    about work
    Python 打印 不换行
  • 原文地址:https://www.cnblogs.com/kongxiaoshuang/p/6512322.html
Copyright © 2020-2023  润新知