HttpClient使用Odata无法正常工作

时间:2018-06-28 03:27:31

标签: xamarin.forms odata

我使用以下代码访问SAP Odata WebService:

     string strUrl = "https://...../HandlingUnitSet?$format=json";

        var credentials = new NetworkCredential("user", "usr123");
        var handler = new HttpClientHandler { Credentials = credentials };
        var client = new System.Net.Http.HttpClient(handler);
        client.DefaultRequestHeaders.Accept.Clear();            
        client.DefaultRequestHeaders.Add("Accept", "application/json");

        var response = await client.GetAsync(strUrl);

        string status = response.StatusCode.ToString();
        string header = response.Headers.ToString();
        string cont = response.Content.ToString();

        string contactsJson = response.Content.ReadAsStringAsync().Result;

             UoUnit ObjUnitList = new UoUnit();
            ObjUnitList = JsonConvert.DeserializeObject<UoUnit>(contactsJson);

问题:我收到以下错误消息

该URL应该返回Json Data,但我没有得到它。在DeserializeObject的行上遇到此错误

解析值<时遇到意外字符。路径'',第0行,位置0。   在Newtonsoft.Json.JsonTextReader.ParseValue()

{Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
  at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
   }

0 个答案:

没有答案