zoukankan      html  css  js  c++  java
  • ASP.NET获取在线接口数据HttpPost

    哈喽,又是新的一周。

    上次说过了HTTPGet的接口参数获取,今天就顺势说下HttpPost的数据获取吧。

    话不多说,Code Is EveryThing

        private static string PostDataJsonBy(string url,string content)
            {
                string result = string.Empty;
                FormUrlEncodedContent body = new FormUrlEncodedContent(new Dictionary<string, string>());
                try
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(content);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                    request.Method = "POST";
                    request.ContentLength = bytes.Length;
                    request.ContentType = "application/json";
                    using (Stream reqstream = request.GetRequestStream())
                    {
                        reqstream.Write(bytes, 0, bytes.Length);
                    }
                    //声明一个HttpWebRequest请求
                    request.Timeout = 90000;
                    //设置连接超时时间
                    request.Headers.Set("Pragma", "no-cache");
                    try
                    {
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                        using (Stream streamReceive = response.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8))
                            {
                                result = streamReader.ReadToEnd();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        result = e.Message;
                    }
                }
                catch (Exception)
                {
                    return "[]";
                }
                return result;
            }

    简简单单,这个就是Post 的数据获取了。

    如果这个帖子到这里就终结,好像也没啥,毕竟介绍都介绍完了。

    但是我们写代码的怎么能就这么粗糙呢?

    上次就说过Get,这次又说了Post,口罩都戴一年了。还分什么你我他啊

       private static string GetDataJsonBy(string url,string content,string method = "Get")
            {
                string result = string.Empty;
                try
                {
                    //声明一个HttpWebRequest请求
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                    //设置连接超时时间
                    request.Timeout = 90000;
                    if (method != "Get")
                    {
                        FormUrlEncodedContent body = new FormUrlEncodedContent(new Dictionary<string, string>());
                        byte[] bytes = Encoding.UTF8.GetBytes(content);
                        request.Method = "POST";
                        request.ContentLength = bytes.Length;
                        request.ContentType = "application/json";
                        using (Stream reqstream = request.GetRequestStream())
                        {
                            reqstream.Write(bytes, 0, bytes.Length);
                            Console.WriteLine(bytes.ToString(), 0, bytes.Length);
                        }
                        request.Headers.Set("Pragma", "no-cache");
                    }
                   
                    try
                    {
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                        using (Stream streamReceive = response.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8))
                            {
                                result = streamReader.ReadToEnd();
                            }
                        }
                       
                    }
                    catch (Exception e)
                    {
                        result = e.Message;
                    }
                }
                catch (Exception)
                {
                    return "[]";
                }
                return result;
            }

    鉴于我出众的大脑,在原有post方法上进行了一些小小的改动(懒)

    于是这个兼顾了Get和Post的方法就成了。

    【其实是下次我不用在写了】

    好了,这次就到这里

  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/SevenWang/p/14059795.html
Copyright © 2011-2022 走看看