zoukankan      html  css  js  c++  java
  • C# Json 常用

    1、MVC返回json方式

      1、(自定义返回json对象)var optionStrCity=“”;var optionStrBank =“”;

    return  Json(new { optionStrCity = optionStrCity, optionStrBank = optionStrBank });

      2、(MVC 对象Json序列化) Object obj=new Object();

    return Json(obj,JsonBehavier.AllowGet);

    2、Json 接收 

      1、(自动映射)

      var postData=$.toJSON(obj);

    List<ContractPostModel> ModelList = JsonConvert.DeserializeObject<List<ContractPostModel>>(System.Web.HttpContext.Current.Request.Form["postData"]); 

    3、C# post 数据

    #region 调取接口 post 数据
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string JsonStr = "JsonDetail=" + jss.Serialize(AppLyDetail);
                string AddUrl = "http://jrpt.fang.com/managerproduct/DataMigrate/AddOnepointzero";
    
                byte[] postData = Encoding.UTF8.GetBytes(JsonStr);//编码,尤其是汉字,事先要看下抓取网页的编码方式  
                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可  
                try
                {
                    byte[] responseData = webClient.UploadData(AddUrl, "POST", postData);//得到返回字符流 
                    string srcString = Encoding.UTF8.GetString(responseData);//解码  
                    if (!string.IsNullOrWhiteSpace(srcString))
                    {
                        if (srcString != "error")
                        {
                            if (OrderType == 1)
                            {
                                Finance_LoanAppPerBLL.UpdateLoanInfoIsTongBu(AppLyDetail.ID, 1, AppLyDetail.OrderNo);
                            }
                            else
                            {
                                Finance_LoanAppAgentBLL.UpdateLoanInfoIsTongBu(AppLyDetail.ID, 1, AppLyDetail.OrderNo);
                            }
                            WriteLog(AppLyDetail.OrderNo + "------执行成功");
                            return 1;
                        }
                        else
                        {
                            WriteLog(AppLyDetail.OrderNo + "------同步失败");
                            return 0;
                        }
                    }
                    else
                    {
                        WriteLog(AppLyDetail.OrderNo + "------同步失败");
                        return 0;
                    }
                }
                catch (Exception)
                {
                    WriteLog(AppLyDetail.OrderNo + "------执行异常");
                    return 0;
                }
                #endregion
    View Code

    4、C# 模拟http请求(POST)

    private string HttpPost(string Url, string postDataStr)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                request.CookieContainer = cookie;
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();
     
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     
                response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
     
                return retString;
            }
    View Code

    5、C# 模拟http请求(GET)

    public string HttpGet(string Url, string postDataStr)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
     
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
     
                return retString;
            }
    View Code
  • 相关阅读:
    [SSRS] Use Enum values in filter expressions Dynamics 365 Finance and Operation
    Power shell deploy all SSRS report d365 FO
    display method in Dynamics 365 FO
    How To Debug Dynamics 365 Finance and Operation
    Computed columns and virtual fields in data entities Dynamics 365
    Azure DevOps for Power Platform Build Pipeline
    Create readonly entities that expose financial dimensions Dynamics 365
    Dataentity call stack dynamics 365
    Dynamics 365 FO extension
    Use singletenant servertoserver authentication PowerApps
  • 原文地址:https://www.cnblogs.com/eric-gms/p/5594184.html
Copyright © 2011-2022 走看看