C# 用 WebClient 的 Post 方法向 WebServer 傳輸數據
阿新 • • 發佈:2019-01-27
tel lte code utf8 urlencode 上傳 exc post webclient
1、比如有一服務
[HttpPost] public ActionResult Index(string str) { try { System.IO.File.WriteAllText(Server.MapPath(@"/test/txt.txt"), str, Encoding.UTF8); return Content("已上傳完成 "); } catch (Exception ex) {return Content(ex.Message); throw; } }
2、使用webclient發出請求
static void Main(string[] args) { WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); var b = Encoding.UTF8.GetBytes("str=我是從webclient來的朋友"); var res = wc.UploadData(@"http://localhost:1863/", "post", b); var result = Encoding.UTF8.GetString(res); Console.WriteLine(result); Console.Read(); }
C# 用 WebClient 的 Post 方法向 WebServer 傳輸數據