zoukankan      html  css  js  c++  java
  • form-data格式、json格式请求服务器接口

    #region form-data格式 
    //start 将实体Contract转换成所需的小驼峰格式(例如:helloWord)
    Contract contract=new Contract();
    contract.ContractName="合同1";
    contract.ContractType="其他合同";
    var setting = new JsonSerializerSettings
    {
    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
    }; 
    var json = JsonConvert.SerializeObject(contract, setting); 
    //end 
    
    
    List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>
    {
    new KeyValuePair<string, string>("archivesElement", json),
    new KeyValuePair<string, string>("dataId", "111111"),
    new KeyValuePair<string, string>("dataCreator", "admin")
    };
    FormUrlEncodedContent formUrlEncodedContent= new FormUrlEncodedContent(paramList); 
    formUrlEncodedContent.Headers.Add("consumerCode", consumerCode);
    formUrlEncodedContent.Headers.Add("operationCode", operationCode);
    var res = client.PostAsync(new Uri(url), formUrlEncodedContent).Result;
    
    Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(res.Content.ReadAsStringAsync().Result);
    Log log = new Log();
    if (dic["code"].ToString()=="100000")
    {
    log.SuccesTime = DateTime.Now;
    log.PushState = 1;
    log.PushMsg = dic["message"].ToString();
    }
    else
    {
    log.SuccesTime = DateTime.Now;
    log.PushState = 2;
    log.PushMsg = dic["message"].ToString();
    }
    #endregion
    form-data格式请求
    #region form-data格式 
    //start 将实体Contract转换成所需的小驼峰格式(例如:helloWord)
    Contract contract=new Contract();
    contract.ContractName="合同1";
    contract.ContractType="其他合同";
    var setting = new JsonSerializerSettings
    {
    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
    }; 
    var json = JsonConvert.SerializeObject(contract, setting); 
    var retrunValue = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json.ToString());
    //end 
    
    HttpContent content = new StringContent(retrunValue.ToString());
    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");           
    content.Headers.Add("consumerCode", consumerCode);
    content.Headers.Add("operationCode", operationCode);
    var res = client.PostAsync(new Uri(url), content).Result;
    
    Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(res.Content.ReadAsStringAsync().Result);
    Log log = new Log();
    if (dic["code"].ToString()=="100000")
    {
    log.SuccesTime = DateTime.Now;
    log.PushState = 1;
    log.PushMsg = dic["message"].ToString();
    }
    else
    {
    log.SuccesTime = DateTime.Now;
    log.PushState = 2;
    log.PushMsg = dic["message"].ToString();
    }
    json格式请求
    //start 将实体Contract转换成所需的小驼峰格式(例如:helloWord)
     Contract contract=new Contract();
     contract.ContractName="合同1";
     contract.ContractType="其他合同"
     var setting = new JsonSerializerSettings
     {
     ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
     }; 
     var json = JsonConvert.SerializeObject(contract, setting); 
     var retrunValue = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json.ToString());
     //end 
    将实体中属性转换成所需小驼峰格式json
    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/***]]
    Server Tomcat v7.0 Server at localhost failed to start.解决方法
    JDBC数据库之添加数据
    angularJS通过post方法下载excel文件
    mybatis使用笔记
    java枚举类型使用笔记
    java8新特性笔记
    Enum枚举类型的使用笔记
    sql server备份还原数据时的问题记录
    SQL Server自增长列插入指定值 -- SET IDENTITY_INSERT ON|OFF(转)
  • 原文地址:https://www.cnblogs.com/YYkun/p/15223322.html
Copyright © 2011-2022 走看看