没有来自web api的回复

时间:2017-02-03 02:07:45

标签: c# rest asp.net-web-api

尝试使用HTTPClient发布到api.lob.com。在调试期间,intellisense在HTTPRequestMessage中显示一个值,但任务httpresponsemessage值根本不显示任何内容。

根据此帖click here

构建代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;

namespace HTTPClientAPICall
{
    class Program
    {
        static void Main(string[] args)
        {

            callAPI();  

        }

    static void callAPI()
    {
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("https://api.lob.com");

        client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new 

AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "test_xxxxxxxxxxxxxxxxxxxxxxxxxx", ""))));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/verify");

            request.Content = new StringContent("{\"address_line1\":\"1600 Pennsylvania Ave NW\",\"address_city\":\"Washington\",\"address_state\":\"DC\",\"address_zip\":\"20500\"}", Encoding.UTF8, "application/json");

            client.SendAsync(request).ContinueWith

                ((responseTask) =>
                  {

                      Console.WriteLine("Response: {0}", responseTask.Result);

                  });

        }
    }
}

智能感知 enter image description here

VS

enter image description here

我已经看过使用RESTSharp但是更喜欢直接使用C#而没有额外的引用。

1 个答案:

答案 0 :(得分:0)

在原始代码中犯了几个错误。这个解决方案正在运行使用这篇文章来确定最终解决方案的起点:click here

解决方案的关键是JSON.NET。

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

namespace JSONandHTTPClient
{
    class Program
    {
        static void Main(string[] args)
        {

            callJSONandHTTPClientApp();

        }

        static void callJSONandHTTPClientApp()
        {
            using (var client = new HttpClient())
            {
                var parameters = new Dictionary<string, string>();
                parameters["address_line1"] = "1600 Pennsylvania Ave NW";
                parameters["address_city"] = "Washington";
                parameters["address_state"] = "DC";
                parameters["address_zip"] = "20050";

                client.BaseAddress = new Uri("https://api.lob.com");

                client.DefaultRequestHeaders
                .Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", ""))));

                var justJSON = JsonConvert.SerializeObject(parameters).ToString();

                var value = new StringContent(justJSON, Encoding.UTF8, "application/json");

                var response = client.PostAsync("v1/verify", value).Result;

                if (response.IsSuccessStatusCode)
                {
                    dynamic content = JsonConvert.DeserializeObject(
                        response.Content.ReadAsStringAsync()
                        .Result);

                    Console.WriteLine(content.ToString());
                }

                Console.ReadLine();
            }

        }
    }
}