• 首页
  • 产品与方案
  • 众成服务
  • 走进众成
  • 新闻中心
  • 企业文化
  • 联系我们
  • 解决方案
  • 众成软件
  • 维护支持
  • 运维服务
  • 技术交流
  • 公司介绍
  • 荣誉资质
  • 合作伙伴
  • 招贤纳士
  • 公司新闻
  • 业界动态
  • 文化建设
  • 企业文化
  • 荣誉榜

首页 > 众成服务 > 技术交流

技术交流

C#跨平台调用接口(URL传参GET调用和Post Json传参调用

 作者:众成   文章来源:软件部    点击数:  更新时间:2019-09-11 09:25:39
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yitonglizihao/article/details/50780453
方法一:URL传参Get调用
 
使用HttpWebRequest,将参数Get调用
 
 
 
  string HampsonURL = new BSysConfig().GetValue(ConfigKey.接口地址);
                HampsonInfo Hmodel = new HampsonInfo();
                Hmodel.cname = ePersonInfo.Name;
                Hmodel.cmobile = ePersonInfo.Mobile;
                Hmodel.cage = ePersonInfo.Age.ToString();
                Hmodel.ccity = ePersonInfo.IntentCity;
                Hmodel.user = "8a9ea098ac0079426ae3c722c8e48d0d";
                string url = string.Format(@"{0}?user={1}&cname={2}&cmobile={3}&cage={4}&ccity={5}"
                           , HampsonURL, Hmodel.user, Hmodel.cname, Hmodel.cmobile, Hmodel.cage, Hmodel.ccity);
                try
                {
                    string resultCode = GetHttp(url, 100000);
                    switch (Convert.ToInt32(resultCode))
                    {
                        case 0:
                            result = "录入成功";
                            break;
                        case 1:
                            result = "此用户已存在";
                            break;
                        case 2:
                            result = "其他错误";
                            break;
                        default:
                            result = "文档木有写是啥错";
                            break;
                    }
                }
                catch (Exception ex)
                {
                    result = "异常:"+ex.ToString();
                }
 
 
 
 
 
   public static string GetHttp(string url, int timeout)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = timeout;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                StringBuilder builder = new StringBuilder();
                while (-1 != reader.Peek())
                {
                    builder.Append(reader.ReadLine());
                }
                return builder.ToString();
            }
            catch (Exception exception)
            {
                return ("错误:" + exception.Message);
            }
        }
 
 
 
方法二:创建对象,序列化为Json格式,在使用WebRequest进行POST调用
 
 
 
string MeilianCPS = new BSysConfig().GetValue(ConfigKey.接口地址);
                string MeilianKEY = new BSysConfig().GetValue(ConfigKey.加密字符);
                MeiLian_CPSInfo Mmodel = new MeiLian_CPSInfo();
                Mmodel.City = ePersonInfo.IntentCity;
                Mmodel.Mobile = ePersonInfo.Mobile;
                Mmodel.Name = ePersonInfo.Name;
                Mmodel.Sex = ePersonInfo.Sex == 1 ? "True" : "False";
                Mmodel.UserName = "cps_shiji";
                string Keys = Mmodel.Mobile + MeilianKEY;
                Mmodel.KEY = EncryptString.EncryptMD5(Keys);//
                string strContent = JsonHelper.Serialize(Mmodel);
                result = GetMobileConfByUserId(strContent, MeilianCPS);
                if (result.Contains("000000"))
                    result = "操作成功";
 
 
 
 
 
        public   string GetMobileConfByUserId(string strContent, string strOnLine)
        {
            string rs = null;
            ServicePointManager.DefaultConnectionLimit = 300;
            CookieContainer cookieContainer = new CookieContainer();
            // 设置提交的相关参数 
            HttpWebRequest request = null;
            HttpWebResponse SendSMSResponse = null;
            Stream dataStream = null;
            StreamReader SendSMSResponseStream = null;
            try
            {
                request = WebRequest.Create(strOnLine) as HttpWebRequest;
                request.Method = "POST";
                request.KeepAlive = false;
                request.ServicePoint.ConnectionLimit = 300;
                request.AllowAutoRedirect = true;
                request.Timeout = 10000;
                request.ReadWriteTimeout = 10000;
                request.ContentType = "application/json";
                request.Accept = "application/xml";
                request.Headers.Add("ProduceAction", HttpUtility.UrlEncode("CustomerIncome"));
                byte[] bytes = Encoding.UTF8.GetBytes(strContent);
                request.Proxy = null;
                request.CookieContainer = cookieContainer;
                using (dataStream = request.GetRequestStream())
                {
                    dataStream.Write(bytes, 0, bytes.Length);
                }
                SendSMSResponse = (HttpWebResponse)request.GetResponse();
                if (SendSMSResponse.StatusCode == HttpStatusCode.RequestTimeout)
                {
                    if (SendSMSResponse != null)
                    {
                        SendSMSResponse.Close();
                        SendSMSResponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                    return null;
                }
                SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                string strRespone = SendSMSResponseStream.ReadToEnd();
                return strRespone;
            }
            catch (Exception ex)
            {
                if (dataStream != null)
                {
                    dataStream.Close();
                    dataStream.Dispose();
                    dataStream = null;
                }
                if (SendSMSResponseStream != null)
                {
                    SendSMSResponseStream.Close();
                    SendSMSResponseStream.Dispose();
                    SendSMSResponseStream = null;
                }
                if (SendSMSResponse != null)
                {
                    SendSMSResponse.Close();
                    SendSMSResponse = null;
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            finally
            {
                if (dataStream != null)
                {
                    dataStream.Close();
                    dataStream.Dispose();
                    dataStream = null;
                }
                if (SendSMSResponseStream != null)
                {
                    SendSMSResponseStream.Close();
                    SendSMSResponseStream.Dispose();
                    SendSMSResponseStream = null;
                }
                if (SendSMSResponse != null)
                {
                    SendSMSResponse.Close();
                    SendSMSResponse = null;
                }
                if (request != null)
                {
                    request.Abort();
                }
            }            
            return rs;
        }
 ————————————————
版权声明:本文为CSDN博主「小马亮哥」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yitonglizihao/article/details/50780453

 
  • 地址:温州市车站大道大诚商厦E幢四楼 | 电话:0577-88891333 | 技术服务电话:4008515159 | 传真:0577-88363999
  • 邮箱:jucher@jucher.com | 浙ICP备05000620号-1
  • Copyright © 2009-2019 JUCHER CORPORATION CO., LTD All Rights Reserve