使用WebClient上传序列化数据
要求简单。我有一个类 - 用户 - {userId,userName,age}。
如何序列化类的对象并使用 webclient 发送到 url(使用 post)。
像下面这样的东西。
将用户对象序列化为 postdata 格式的最佳方法是什么?
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
string postData = "orderId=5&status=processed2&advertId=ADV0001a";
byte[] postArray = Encoding.ASCII.GetBytes(postData);
client.Headers.Add("Content-Type","application/x-www-form-urlencoded");
byte[] responseArray = client.UploadData(address, postArray);
var result = Encoding.ASCII.GetString(responseArray);
return result;
Simple requirement . I have a class - User - {userId, userName, age}.
How to serailize a object of the class and sent to a url (using post) using webclient.
Something Like below.
What is the best way to serialise user object into postdata format.
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
string postData = "orderId=5&status=processed2&advertId=ADV0001a";
byte[] postArray = Encoding.ASCII.GetBytes(postData);
client.Headers.Add("Content-Type","application/x-www-form-urlencoded");
byte[] responseArray = client.UploadData(address, postArray);
var result = Encoding.ASCII.GetString(responseArray);
return result;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将对您的代码应用以下简化:
I would apply the following simplification to your code: