• 后台post get请求


    /// <summary>
    /// 执行HTTP POST请求。
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="parameters">请求参数</param>
    /// <returns>HTTP响应</returns>
    public static string DoPost(string url, IDictionary<string, string> parameters)
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.KeepAlive = true;
    req.UserAgent = "59MiaoApp";
    req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

    byte[] postData = Encoding.UTF8.GetBytes(BuildPostData(parameters));
    Stream reqStream = req.GetRequestStream();
    reqStream.Write(postData, 0, postData.Length);
    reqStream.Close();

    HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
    Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
    return GetResponseAsString(rsp, encoding);
    }

    /// <summary>
    /// 执行HTTP GET请求。
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="parameters">请求参数</param>
    /// <returns>HTTP响应</returns>
    public static string DoGet(string url, IDictionary<string, string> parameters)
    {
    if (parameters != null && parameters.Count > 0)
    {
    if (url.Contains("?"))
    {
    url = url + "&" + BuildPostData(parameters);
    }
    else
    {
    url = url + "?" + BuildPostData(parameters);
    }
    }


    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "GET";
    req.KeepAlive = true;
    req.UserAgent = "Top4Net";
    req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";


    HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
    Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
    return GetResponseAsString(rsp, encoding);
    }

  • 相关阅读:
    字和字节和位的关系
    什么是arp协议?
    Python解释器有哪些?Python解释器种类
    简述解释型和编译型编程语言?
    Python3基础(五) 函数
    Python3基础(四) 条件与循环控制
    Python3基础(三) 运算符
    Python3基础(一) Hello World
    1987年国际C语言混乱代码大赛获奖的一行代码
    C++学习之普通函数指针与成员函数指针
  • 原文地址:https://www.cnblogs.com/-maomao/p/5219522.html
Copyright © 2020-2023  润新知