zoukankan      html  css  js  c++  java
  • asp.net 后台 get,post请求


    //Post请求
    public static string Post(string url,string obj=null)
    {
    string param = (obj);//参数
    byte[] bs = Encoding.Default.GetBytes(param);

    //创建一个新的HttpWebRequest对象。
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);

    // 将方法属性设置为“POST”以将数据发布到URI。
    req.Method = "POST";

    //设置contentType属性。
    req.ContentType = "application/json";

    req.ContentLength = bs.Length;
    using (Stream reqStream = req.GetRequestStream())
    {
    reqStream.Write(bs, 0, bs.Length);

    reqStream.Close();
    HttpWebResponse response2 = (HttpWebResponse)req.GetResponse();
    StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.UTF8);
    string text2 = sr2.ReadToEnd();
    return text2;
    }

    }


    //Get请求
    public static string Get(string func, string strParam = null)
    {
    string result = "";
    StringBuilder realUrl = new StringBuilder();
    realUrl.Append(func).Append("?");
    StringBuilder param = new StringBuilder();

    if (strParam != null)
    {
    if (param.Length > 0)
    {
    param.Append("&");
    }
    param.Append(strParam);
    }
    realUrl.Append(param.ToString());
    WebRequest req = WebRequest.Create(realUrl.ToString());
    req.ContentType = "text/html; charset=GBK";

    WebResponse res = req.GetResponse();
    Stream stream = res.GetResponseStream();

    using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312")))
    {
    result = reader.ReadToEnd();
    }
    return result;
    }


    //Get请求
    public static string Get(string func)
    {
         using (System.IO.StreamReader reader = new System.IO.StreamReader(WebRequest.Create(func).GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
         {
             return reader.ReadToEnd();
        }
    }

  • 相关阅读:
    MPLS 知识要点1
    ISIS的SSN和SRM标志
    对比ISIS和OSPF
    ISIS帧中继实验
    ISIS 认证实验
    ISIS数据库同步
    ISIS Lab 路由泄露
    ISIS Lab 重分布直连
    32、端口有效范围是多少到多少?
    33、为何需要把 TCP/IP 协议栈分成 5 层(或7层)?开放式回答。
  • 原文地址:https://www.cnblogs.com/zengwangjing/p/7644422.html
Copyright © 2011-2022 走看看