简体   繁体   English

如何将Json对象序列化为System.Net.WebClient(.NET 3.5)

[英]How to serialize Json object into System.Net.WebClient (.NET 3.5)

How can you serialize a json object and pass it on to an api call, like the one in the example posted as an answer here Call web APIs in C# using .NET framework 3.5 您如何序列化一个json对象并将其传递给api调用,如示例中的示例作为此处的答案发布的那样。 使用.NET Framework 3.5在C#中调用Web API

System.Net.WebClient client = new System.Net.WebClient();
 client.Headers.Add("content-type", "application/json");//set your header here, you can add multiple headers
 string s = Encoding.ASCII.GetString(client.UploadData("http://localhost:1111/Service.svc/SignIn", "POST", Encoding.Default.GetBytes("{\"EmailId\": \"admin@admin.com\",\"Password\": \"pass#123\"}")));

In Postman I would just do this 在邮递员中,我会这样做

 var client = new RestClient("");
 var request = new RestRequest(Method.POST);
 request.JsonSerializer = new RestSharpJsonNetSerializer();
 request.AddJsonBody(JsonObject);

However, as Postman is not supported in .net framework 3.5, I have to use System.Net.WebClient. 但是,由于.net framework 3.5不支持Postman,因此我必须使用System.Net.WebClient。

You can do what you want with WebClient (and Json.NET package) like this: 您可以使用WebClient (和Json.NET软件包)来完成所需的操作,如下所示:

var yourObject = new YourObject {
    Email = "email",
    Password = "password"
};
string s = client.UploadString("http://localhost:1111/Service.svc/SignIn","POST", JsonConvert.SerializeObject(yourObject));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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