[英]Deserializing HttpWebResponse from JSON API in c#
I know that such of these topics existing a lot here on stackoverflow. 我知道这些主题在stackoverflow上已经存在很多。 But I have a little bit different problem:
但是我有一点不同的问题:
I have created a DataContract Class like below: 我创建了一个DataContract类,如下所示:
[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. 我从响应标头中的JSON API得到了状态代码为201的答案“已创建”。 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 "."."
但是,当我获得ResponseStream时,出现错误“ System.Runtime.Serialization.SerializationException-反序列化对象时出错。意外的字符“。”。 .
。
Alternative I have tested it with the Mozilla REST client. 替代方案我已经使用Mozilla REST客户端对其进行了测试。 And I will get the correct header with a correct response stream.
我将获得具有正确响应流的正确标头。
The response stream included also a base64 string encoded pdf document. 响应流还包括base64字符串编码的pdf文档。
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响应的一小部分:
If I'm opening the JSON quick view at the monitoring window, I just get the message back "string is not JSON formated". 如果要在监视窗口中打开JSON快速查看,我只会收到“字符串不是JSON格式”的消息。
Milo 米洛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.