c#中的rest call post方法

时间:2018-03-23 10:28:19

标签: c# asp.net rest

我正在为get和post方法编写restcalls。这是代码。

public class RestCall
    {
        public static string loginJsonString;
        public static string xmlResult;

        // GET: RestCall
        public async Task RunAsync(string name, string value)
        {
            using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
            using (var client = new HttpClient(handler))
            {
                var byteArray = Encoding.ASCII.GetBytes("username:password");
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                var urlRestGet = HomeController.url;
                client.BaseAddress = new Uri(urlRestGet + "?name=" + name + "&value=" + value + "");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.GetAsync(urlRestGet + "?name=" + name + "&value=" + value + "");

                if (response.IsSuccessStatusCode)
                {
                    //Get the response
                    loginJsonString = await response.Content.ReadAsStringAsync();

                    //Converting to xml
                    using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(loginJsonString)))
                    {
                        var output = new XmlDictionaryReaderQuotas();
                        xmlResult=XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, output)).ToString();
                    }
                }
            }
        }
        //Post
        public async Task RunAsync()
        {
            using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
            using (var client = new HttpClient(handler))
                {
                    HttpResponseMessage response;
                    Vehicle vehicle = new Vehicle();
                    vehicle.Model = "Vehicle1";
                    vehicle.Description = "Description1ForVehicle1";
                    vehicle.ETag = "";
                    response = await client.PostAsJsonAsync("xxx",vehicle);
                    if(response.IsSuccessStatusCode){
                        Uri vehicleUri = response.Headers.Location;
                    }
                }
            }
    }
}

我的get方法运行正常。但post方法抛出异常“无法加载文件或程序集'Newtonsoft.Json,Version = 4.5.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'或其中一个依赖项。系统找不到指定的文件。”

1)如果是newtonsoft错误,为什么我的get方法有效?

2)我该如何解决这个问题?

我的web.config文件显示,

    <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" culture="neutral" 
             publicKeyToken="30ad4fe6b2a6aeed" />
            <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
   </dependentAssembly>

1 个答案:

答案 0 :(得分:2)

您的项目中似乎没有引用Newtonsoft.Json程序集。

这可能是由此造成的:

  1. 如果您(强制)从项目中删除了Newtonsoft.Json Nuget包。
  2. 如果您的绑定重定向错误(请检查 web.config
  3. 错误的程序集被加载到 / bin ,这可能是由错误的 HintPath 属性引起的
  4. 最后,您的 Read 方法有效,因为您不会使用Newtonsoft.Json来反序列化您获得的响应。但是, Post 方法会导致异常,因为您使用的是PostAsJsonAsync方法,它依赖于Newtonsoft.Json库。