使用httpclient異步調用WebAPI接口


最近的工作需要使用Bot Framework調用原有的WebAPI查詢數據,查找了一些方法,大部分都是使用HttpClient調用的,現時貼出代碼供參考

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Text;
using Newtonsoft.Json;
using System.Net;

namespace esquel_LPD_Bot.LPDService
{
    public class GarmentStyleHelper
    {
        public async Task<string> GarmentStyleSearch(string pi_GarmentStyleNo)
        {
            string l_returnValue = string.Empty;
            string l_APIUrl = Common.ConfigHelper.GetConfigValue("LPD-GarmentStyleSearchAPIUrl");
            Common.LPDAuthHelper LPDAuthClass = new Common.LPDAuthHelper();

            try
            {
                if(string.IsNullOrEmpty( LPDAuthClass.UserName)|| string.IsNullOrEmpty(LPDAuthClass.UserName))
                {
                    throw new Exception("LPD Project Auth User Name Or Password Is Empty .");
                }

                if (!string.IsNullOrEmpty(l_APIUrl) && !string.IsNullOrEmpty(pi_GarmentStyleNo))
                {
                    var request = new Model.SelectionFilter()
                    {
                        FilterType = Model.SelectionFilter.FilterTypeLeaf,
                        Filters = new Model.SelectionFilter[] { },
                        AttributeName = "item_number",
                        FilterValue = pi_GarmentStyleNo
                    };
                    //--------------------------------------------------------------------------------------------

                    var handler = new HttpClientHandler()
                    {
                        AutomaticDecompression = System.Net.DecompressionMethods.GZip
                    };
                    //handler.UseDefaultCredentials = true;//use default network利用的用戶本機的網絡,但因為需要布署到服務器,所以使用以下這一句
                    handler.Credentials=new NetworkCredential(LPDAuthClass.UserName, LPDAuthClass.PassWord);//設定權限,這一句比較痛苦,找了很久

                    using (var client = new HttpClient(handler))
                    {
                        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));                       

                        var response = await client.PostAsync(l_APIUrl, new StringContent(JsonConvert.SerializeObject(request).ToString(), Encoding.UTF8, "application/json"));
                        response.EnsureSuccessStatusCode();
                        string l_getreturn = await response.Content.ReadAsStringAsync();

                        l_returnValue = l_getreturn;
                    }
                }
            }
            catch (Exception ex)
            {
                
            }

            return l_returnValue;
        }
    }
}

 參考地址:

http://www.cnblogs.com/validvoid/p/demystifying-httpclient-apis-in-the-uwp.html 使用身份驗證憑據

http://www.cnblogs.com/lori/p/4045633.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM