zoukankan      html  css  js  c++  java
  • WebAPI学习笔记(3)Asp.net调用WebAPI Post方法传递参数

    1、WebAPI方法:

    [HttpPost]
    public HttpResponseMessage ImportIssue(dynamic obj)
    {
                MethodReturnModel<string> returnModel = new MethodReturnModel<string>();
                IssueModel issueModel = new IssueModel();
    
                try
                {
                    string IssueJsonStr = obj.IssueJson.ToString();
    
                    issueModel = ConvertJson.JsonToObject<IssueModel>(IssueJsonStr);
                    if(issueModel == null || string.IsNullOrWhiteSpace(issueModel.IssueKey))
                    {
                        returnModel.Result = false;
                        returnModel.Message = "The json string of issue is not correct";
                    }
                    else
                    {
                        IssueBLL issueBLL = new IssueBLL(AdminUserToken);
                        string ErrorMessage = "";
                        if (issueBLL.CreateGPISIssue(issueModel, ref ErrorMessage) > 0)
                        {
                            returnModel.Result = true;
                        }
                        else
                        {
                            returnModel.Result = false;
                            returnModel.Message = ErrorMessage;
                        }
                    }
                }
                catch(Exception ex)
                {
                    returnModel.Result = false;
                    returnModel.Message = ex.Message;
                }
    
                return MethodHelper.GetHttpResponseMessage(ConvertJson.GetJson<MethodReturnModel<string>>(returnModel));
    }

    2、调用方式:

    string Username = "xxx";
    string Password = "xxx";
    string Body = "{'IssueJson': { 'IssueKey': '009','IssueType': '111'}}";
    using (HttpClient client = new HttpClient())
    {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}")));
    
                    HttpContent httpContent = new StringContent(Body, Encoding.UTF8);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
                    Uri address = new Uri("https://localhost:44300/api/issue/ImportIssue");
    
                    var response = client.PostAsync(address, httpContent).Result.Content.ReadAsStringAsync().Result;//返回值
    }

    3、返回结果:

    【原文出处】http://www.51aras.com/?id=41

      

  • 相关阅读:
    原型模型
    V模型
    瀑布模型
    微服务的特点 优点 缺点
    ip地址的分类
    DSSA特定领域软件体系结构
    Git操作 :从一个分支cherry-pick多个commit到其他分支
    【原理】从零编写ILI9341驱动全过程(基于Arduino)
    Arduino驱动ILI9341彩屏(一)——颜色问题
    STL库学习笔记(一)——什么是STL?
  • 原文地址:https://www.cnblogs.com/61007257Steven/p/11759497.html
Copyright © 2011-2022 走看看