• 使用 HttpWebRequest的psot方式传参数调用接口方法


     public ContentResult UseHttpWebRquust(string url,string param)
            {
                HttpWebResponse response = null;
                Stream stream = null;
                StreamReader streamReader = null;
                Stream responseStream = null;
                string jsontext = string.Empty;
                    try
                    {
                        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                        request.Method = "POST";
                        request.Accept = "*/*";
                        request.ContentType = "multipart/byteranges";//根据需要设置请求内容类型
                        request.Timeout = 50000;
                        request.KeepAlive = true;//建立永久链接
                        request.ProtocolVersion = HttpVersion.Version10;
                        //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)";
                        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                       
    
                        byte[] data = Encoding.Default.GetBytes(param);
                        request.ContentLength = data.Length;
    
                        using (stream = request.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
    
                        }
                        response = request.GetResponse() as HttpWebResponse;
                        responseStream = response.GetResponseStream();
                        streamReader = new StreamReader(responseStream, Encoding.UTF8);
                        SmsResult smsResult = Newtonsoft.Json.JsonConvert.DeserializeObject<SmsResult>(jsontext);
                        result = smsResult.resultCode;
                        if (responseStream != null)
                        {
                            responseStream.Close();
                        }
                        if (streamReader != null)
                        {
                            streamReader.Close();
                        }
                        if (stream != null)
                        {
                            stream.Close();
                        }
                        //关闭stream的顺序要正确
                    }
                    catch (System.Exception ex)
                    {
                        return Content(result.ToString());
                    }
                    return Content(result.ToString());
               
            }
     
  • 相关阅读:
    重构DataGridView的方法
    Js中得到radiobuttonlist 和CheckBoxList 的值
    01、Android系统系统架构
    10、JavaEEDBUtils工具类
    07、JavaEEJSP自定义标签
    11、JavaEEFilter
    08、JavaEEMysql基础
    09、JavaEEJDBC
    简单易忘哈哈(sql语句中的空格问题)
    定义Connection对象con的好处
  • 原文地址:https://www.cnblogs.com/jeemly/p/4043292.html
Copyright © 2020-2023  润新知