• C# WebService服务Post提交


    public string WebServerTest(string PostData)
            {
                PostData = "jsonData=" + PostData;
                string POSTURL = "http://101.201.78.100:8888/ChannelWebService.asmx/CheckIsConnected";
                Encoding myEncoding = Encoding.GetEncoding("UTF-8"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; //将提交的字符串数据转换成字节数组 //注意提交的编码,这里默认的是Default:系统当前编码 
                byte[] postData = myEncoding.GetBytes(PostData); //设置提交的相关参数 
                HttpWebRequest request = WebRequest.Create(POSTURL) as HttpWebRequest; //实例化一个证书 
                //X509Certificate2 cerCaiShang = GetCertificate(); ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; 
                //request.ClientCertificates.Add(cerCaiShang); 
                request.Method = "POST"; request.KeepAlive = true;
                //request.AllowAutoRedirect = true;
                request.ProtocolVersion = HttpVersion.Version10;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                request.ContentType = "text/xml;charset=utf-8";
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.ContentLength = postData.Length;
                #region 传输数据
                try { using (Stream writer = request.GetRequestStream()) { writer.Write(postData, 0, postData.Length); } }
                catch (Exception ex) { return ex.Message; }
                #endregion
                #region 接收返回的数据
                string srcString = string.Empty; try { HttpWebResponse response = request.GetResponse() as HttpWebResponse; using (Stream sr = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(sr, myEncoding)) { srcString = reader.ReadToEnd(); } } }
                catch (Exception ex) { return ex.Message; }
                #endregion
                return srcString;
            }
    

      

  • 相关阅读:
    第51天 [js] 字符串相连有哪些方式?哪种最好?为什么?
    第52天 [js] 什么是事件委托?它有什么好处?能简单的写一个例子吗?
    np.ndarray与Eigen::Matrix之间的互相转换
    C++向assert加入错误信息
    CeiT:训练更快的多层特征抽取ViT
    CoAtNet: 90.88% Paperwithcode榜单第一,层层深入考虑模型设计
    正式启用Danube 官方站点
    go 编译报错 package embed is not in GOROOT (/usr/local/go/src/embed)
    cloudreve兼容acme.sh脚本
    Git的交叉编译
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/6025290.html
Copyright © 2020-2023  润新知