• 窗体调用第三方接口


    一.

            private void button1_Click(object sender, EventArgs e)
            {
                string url = "localhost";
                byte[] responseArray = null;
                using (WebClient webClient = new WebClient())
                {
                    webClient.Headers.Add("jsonStr", "");
                    NameValueCollection postValues = new NameValueCollection();
                    responseArray = webClient.UploadValues(url, postValues);
                }
                string response = Encoding.UTF8.GetString(responseArray);
                if (!string.IsNullOrEmpty(response) && !response.Equals("null"))
                {
                    //result = JsonConvert.DeserializeObject<BaseElseResut>(response);
                    //if (result != null && result.Data != null && result.Data.success)
                    //{
                    //    Response.Write("<script language=javascript>window.location.href='" + result.Data.Reason + "';</script>");
                    //}
                    //else
                    //{
                    //    Response.Write("<script>alert('" + result.Data.Reason + "');</script>");
                    //}
                }
            }

    二、

    public string aa()
    {
    string url = "http://localhost";
    byte[] responseArray = null;
    using (WebClient webClient = new WebClient())
    {
        webClient.Headers.Add("usercode", "a");
        webClient.Headers.Add("username", "b");
        NameValueCollection postValues = new NameValueCollection();
        postValues.Add("a", a);
        postValues.Add("b", b);
        postValues.Add("c", c);
        postValues.Add("d", d);
        postValues.Add("c", c);
        responseArray = webClient.UploadValues(url, postValues);
    }
    string response = Encoding.UTF8.GetString(responseArray);
    if (!string.IsNullOrEmpty(response) && !response.Equals("null"))
    {
     result = JsonConvert.DeserializeObject<BaseElseResut>(response);
    }
    }

      

    三、

    public void  PostTest()
    {
        string url = "http://localhost:8009/";
        Dictionary<string, object> dic = null;
        string strParams = JsonConvert.SerializeObject(dic);//ParsToString(pairs);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //请求方式POST
        request.Method = "POST";
        //传输数据JSON类型
        request.ContentType = "application/json;charset=utf-8";
        request.Credentials = CredentialCache.DefaultCredentials;
        byte[] bytes = Encoding.Default.GetBytes("userName:Password");
        var strAuthorization = Convert.ToBase64String(bytes);
        request.Headers.Add("Authorization", strAuthorization);
        //请求超时时间,10分钟
        request.Timeout = 600000;
        AddHeaderParams(request);
        //写入数据
        var data = Encoding.UTF8.GetBytes(strParams);
        request.ContentLength = data.Length;
        Stream writer = request.GetRequestStream();
        writer.Write(data, 0, data.Length);
        writer.Close();
        //请求返回数据
        StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
        string ret = reader.ReadToEnd();
        reader.Close();
    }
    
    /// <summary>
    /// 添加通用的Header参数
    /// </summary>
    /// <returns></returns>
    private void AddHeaderParams(HttpWebRequest request)
    {
        Dictionary<string, string> pairs = new Dictionary<string, string>() {
            { "a", "1" } ,
            { "b", "2" },
            { "c", "3" },
            { "d", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
        };
        foreach (string key in pairs.Keys)
        {
            request.Headers.Add(key, pairs[key]);
        }
    }

    四、

    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    win10一键访问更改适配器的方法
    windows server 2019 hyper-v+zabbix3.4
    Hyper-V 配置虚拟网络
    ros routeros pppoe一直拨号获取到不一样的IP为止。
    usb3.0 3.1 3.2的区别和联系
    西部数据绿盘、蓝盘、黑盘、红盘和紫盘的区别
    Ros-routeros winbox for win,mac,android,ios客户端大全
    《高效能人士的七个习惯》:好的时间管理,每天只做6件事
    快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
    Kubernetes中分布式存储Rook-Ceph的使用:一个ASP.NET Core MVC的案例
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/13343416.html
Copyright © 2020-2023  润新知