在C#中通过WebClient将JSON发布到URL

在C#中通过WebClient将JSON发布到URL,第1张

在C#中通过WebClient将JSON发布到URL

您需要一个json序列化程序来解析您的内容,也许您已经有了它,对于有关如何发出请求的最初问题,这可能是一个主意

var baseAddress = "http://www.example.com/1.0/service/action";var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));http.Accept = "application/json";http.ContentType = "application/json";http.Method = "POST";string parsedContent = <<PUT HERE YOUR JSON PARSED CONTENT>>;ASCIIEncoding encoding = new ASCIIEncoding();Byte[] bytes = encoding.GetBytes(parsedContent);Stream newStream = http.GetRequestStream();newStream.Write(bytes, 0, bytes.Length);newStream.Close();var response = http.GetResponse();var stream = response.GetResponseStream();var sr = new StreamReader(stream);var content = sr.ReadToEnd();

希望能帮助到你,



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5585834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存