如何将C#RestSharp转换为C#ASP.NET httpclient

如何将C#RestSharp转换为C#ASP.NET httpclient,c#,asp.net-mvc-4,httpclient,restsharp,C#,Asp.net Mvc 4,Httpclient,Restsharp,下面的代码是从postman生成的c#restsharp代码。如何将其转换为httpclient的c#asp.net mvc4代码。当尝试在VS中调试时,我总是出现错误请求。这是用于创建发货履行的ebay api。在邮递员作品中测试,代码如下 var client = new RestClient("https://api.ebay.com/sell/fulfillment/v1/order/23-00000-11313/shipping_fulfillment"); client.Timeou

下面的代码是从postman生成的c#restsharp代码。如何将其转换为httpclient的c#asp.net mvc4代码。当尝试在VS中调试时,我总是出现错误请求。这是用于创建发货履行的ebay api。在邮递员作品中测试,代码如下

var client = new RestClient("https://api.ebay.com/sell/fulfillment/v1/order/23-00000-11313/shipping_fulfillment");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Content-Language", "en-US");
request.AddHeader("Accept-Language", "en-US");
request.AddHeader("Accept-Charset", "utf-8");
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer v^1.1#i^1#I^3#f^0#p^3#r^0#t^H4sIAAAAA==");
request.AddParameter("application/json", "{\"lineItems\":[{\"lineItemId\":\"10022882297723\",\"quantity\":1}],\"shippedDate\":\"2020-02-11T01:28:16.475Z\",\"shippingCarrierCode\":\"Couriers Please\",\"trackingNumber\":\"UHV3436755010009000000\"}",  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
asp.net中的当前代码,但请求错误

private HttpClient CreateHttpClient()
{
    var client = new HttpClient();
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    string baseAddress = WebApiBaseAddress;
    if (string.IsNullOrEmpty(baseAddress))
    {
        throw new HttpRequestException("There is no base address specified in the configuration file.");
    }
    client.Timeout = new TimeSpan(0, 5, 59);
    client.BaseAddress = new Uri(baseAddress);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _cred.eBayToken));
    client.DefaultRequestHeaders.Add("Accept-Language", "en-US");
    client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8");
    client.DefaultRequestHeaders.Add("Accept", "application/json");
    client.DefaultRequestHeaders.Add("LegacyUse", "true");
    return client;
}

public HttpResponseMessage PostHttpResponse(string requestUri, object data)
{
    var stringPayload = JsonConvert.SerializeObject(data);
    var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    httpContent.Headers.Add("Content-Language", "en-US");
    httpContent.Headers.Add("Content-Encoding", "gzip");


    using (var client = CreateHttpClient())
    {
        try
        {
            HttpResponseMessage response = client.PostAsJsonAsync(requestUri, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                return response;
            }
            else
            {
                GetErrorsResponse(response);
                throw new HttpRequestException(string.Format("There was an exception trying to post a request. response: {0}", response.ReasonPhrase));
            }
        }
        catch (HttpRequestException ex)
        {
            throw ex;
            //return null;
        }
    }
}

提前感谢你的帮助。非常感谢。

在运行时,您的
基地址是什么?为什么不使用restsharp?