webclient 和httpclient 應用

來源:互聯網
上載者:User

標籤:

//webclient應用
MyImageServerEntities db = new MyImageServerEntities(); public ActionResult Index() { return View(); } public ActionResult FileUpload() { HttpPostedFileBase file = Request.Files["fileUp"]; string fileName = Path.GetFileName(file.FileName); string fileExt = Path.GetExtension(fileName); if (fileExt == ".jpg") { var list=db.ImageServerInfo.Where<ImageServerInfo>(u => u.FlgUsable == true).ToList();//找出可用的圖片伺服器. int count = list.Count(); Random random = new Random(); int r = random.Next(); int i = r % count; string serverUrl = list[i].ServerUrl; int serverId = list[i].ServerId; string url = "http://"+serverUrl+"/FileUp.ashx?serverId="+serverId+"&fileExt="+fileExt; WebClient webClient =new WebClient(); webClient.UploadData(url, StreamToBytes(file.InputStream)); } return Content("ok"); } public ActionResult ShowImage() { var list= db.ImageServerInfo.Where<ImageServerInfo>(c=>c.FlgUsable==true).ToList(); ViewData["imageServerList"] = list; return View(); } private byte[] StreamToBytes(Stream stream) { byte[]buffer=new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); stream.Seek(0, SeekOrigin.Begin); return buffer; }

           context.Response.ContentType = "text/plain";            int serverId = int.Parse(context.Request["serverId"]);            string fileExt = context.Request["fileExt"];            string dir = "/ImagePath/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";            Directory.CreateDirectory(Path.GetDirectoryName(context.Request.MapPath(dir)));            string newfileName = Guid.NewGuid().ToString();            string fullDir = dir + newfileName + fileExt;            using (FileStream fileStream = File.Open(context.Request.MapPath(fullDir), FileMode.OpenOrCreate))            {                context.Request.InputStream.CopyTo(fileStream);                MyImageServerEntities db = new MyImageServerEntities();                ImageInfo imageInfo = new ImageInfo();                imageInfo.ImageName = fullDir;                imageInfo.ImageServerId = serverId;                db.ImageInfo.Add(imageInfo);                db.SaveChanges();            }

  

 

 

//httpclient 應用

建立並初始化對象:    client.BaseAddress = new Uri(url);    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));讀集合:    HttpResponseMessage response = client.GetAsync(url).Result;     var userList = response.Content.ReadAsAsync<IEnumerable<資料類型>>().Result;根據編號讀對象    HttpResponseMessage response1 = client.GetAsync(url).Result;    var userInfo = response1.Content.ReadAsAsync<資料類型>().Result;增加:    HttpResponseMessage response = client.PostAsJsonAsync("api/userinfo", userInfo).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值修改:     HttpResponseMessage response = client.PutAsJsonAsync("api/userinfo/"+userInfo.UserId, userInfo).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值刪除:    HttpResponseMessage response = client.DeleteAsync("api/userinfo/" + uid).Result;    使用response.IsSuccessStatusCode判斷是否成功    使用response.Content.ToString()擷取傳回值

 

webclient 和httpclient 應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.