简体   繁体   中英

Deserializing HttpWebResponse from JSON API in c#

I know that such of these topics existing a lot here on stackoverflow. But I have a little bit different problem:

I have created a DataContract Class like below:

    [DataContract]
  public class GLSParcelClass {
    internal string Location;
    internal string ConsignmentId;
    internal string Labels;
    internal List<Parcels> ParcelList;
    internal List<Returns> ReturnList;
  }

  [DataContract]
  public class Parcels {
    internal string Location;
    internal string TrackId;
    internal string ParcelNumber;
  }

  [DataContract]
  public class Returns {
    internal string Location;
    internal string TrackId;
    internal string ParcelNumber;
  }

Then I wrote the following function:

      WebRequest httpWebRequest = WebRequest.Create(this.GLSUri);
  string json = "{" +
      "\"shipperId\":\"2764200001 276a165X14\"," +
      "\"references\":[\"47110815\"]," +
      "\"addresses\":{" +
        "\"delivery\":{" +
          "\"name1\":\"Max Meyer\"," +
          "\"name2\":\"Meyer Ltd.\"," +
          "\"street1\":\"Meyer Str. 227\"," +
          "\"street2\":\"2. Floor\"," +
          "\"country\":\"DE\"," +
          "\"zipCode\":\"92753\"," +
          "\"city\":\"Passau\"," +
          "\"email\":\"maxmeyer@gmail.com\"}}," +
      "\"parcels\":[" +
        "{" +
          "\"weight\":2.5," +
          "\"references\":[" +
            "\"47110815\"]" +
        "}" +
      "]" +
    "}";

  httpWebRequest.ContentType = "application/json";

  Type type = httpWebRequest.Headers.GetType();
  System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
  System.Reflection.MethodInfo m = type.GetMethod("AddWithoutValidate", flags);
  m.Invoke(httpWebRequest.Headers, new string[] { "Accept", "application/json" });
  m.Invoke(httpWebRequest.Headers, new string[] { "Accept-Language", "de" });
  m.Invoke(httpWebRequest.Headers, new string[] { "Accept-Encoding", "gzip,deflate" });

  httpWebRequest.Method = "POST";

  string lsEncodedCred = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("utf-8").GetBytes("shipmentsapi" + ":" + "shipmentsapi"));
  httpWebRequest.Headers.Add("Authorization", "Basic " + lsEncodedCred);
  httpWebRequest.PreAuthenticate = true;

  StreamWriter streamWriter = null;
  using (streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
  }

  HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

  using (StreamReader loStream = new StreamReader(httpResponse.GetResponseStream())) {
    GLSParcelClass deserializedParcels = new GLSParcelClass();
    string lsJSON = loStream.ReadToEnd();
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(lsJSON));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedParcels.GetType());
    deserializedParcels = ser.ReadObject(ms) as GLSParcelClass;
    ms.Close();
  }

I got the answer "Created" with status code 201 from the JSON API in the response header. So far so good. But when I will get the ResponseStream I get the error "System.Runtime.Serialization.SerializationException - Error while deserializing the object. Unexpected char "."." .

Alternative I have tested it with the Mozilla REST client. And I will get the correct header with a correct response stream.

The response stream included also a base64 string encoded pdf document.

I really don't know what's wrong and I hope you can help me.

Thx a lot in advance.

Milo

I just be able to post a very small part of the JSON response as on the screenshot:

JSON response part

If I'm opening the JSON quick view at the monitoring window, I just get the message back "string is not JSON formated".

Milo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM