我将通过C#中的HttpClient发送序列化的Json,但是服务器看到的是一个空对象。
主要的工作部分如下所示:
我还使用了一个辅助函数将类序列化为MemoryStream,然后使用该字符串从StreamContent类创建我的HttpContent。
以下是 Resting API 需要的内容:
据我所知,这是创建json文件并将其发送到REST API的正确方法。然而,服务器仍将其读取为空。任何帮助都将不胜感激!
主要的工作部分如下所示:
public void create_user(string Email, string Pass)
{
try {
login_information user = new login_information (){ email = Email, pass = Pass };
MemoryStream stream = new MemoryStream ();
json_functions.serializer (user, user.GetType (), ref stream);
HttpContent content = new StreamContent (stream);
HttpResponseMessage response = client.PostAsync ("user/create", content).Result;
if (response.IsSuccessStatusCode) {
}
} catch (Exception e) {
string err = e.ToString ();
}
}
我还使用了一个辅助函数将类序列化为MemoryStream,然后使用该字符串从StreamContent类创建我的HttpContent。
这是json序列化辅助函数:
static public void serializer (Object o, Type T, ref MemoryStream S)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer (T);
s.WriteObject (S, o);
}
被序列化的类:
[DataContract]
public class login_information
{
[DataMember]
public string email;
[DataMember]
public string pass;
}
以下是 Resting API 需要的内容:
{
email:"email@example.com",
pass:"$ExamPLePasSworD$"
}
据我所知,这是创建json文件并将其发送到REST API的正确方法。然而,服务器仍将其读取为空。任何帮助都将不胜感激!