//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, , buffer.Length);
stream.Seek(, 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 应用的更多相关文章

  1. HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别

    HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...

  2. webrequest、httpwebrequest、webclient、HttpClient 四个类的区别

    一.在 framework 开发环境下: webrequest.httpwebreques  都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...

  3. .Net5下WebRequest、WebClient、HttpClient是否还存在使用争议?

    WebRequest.WebClient.HttpClient 是C#中常用的三个Http请求的类,时不时也会有人发表对这三个类使用场景的总结,本人是HttpClient 一把梭,也没太关注它们的内部 ...

  4. WebClient和HttpClient, 以及webapi上传图片

    httppost请求. applicationkey/x-www-form-urlencoded请求: Email=321a&Name=kkfewwebapi里面, 如果用实体, 能接受到. ...

  5. WebClient vs HttpClient vs HttpWebRequest

    转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ Just when I was startin ...

  6. C#中HttpWebRequest、WebClient、HttpClient的使用

    HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如  ...

  7. WebClient 与HttpClient 的区别

    需要搜索下资料. -------------------------------------------------- 微软文档介绍,新的开发中推荐使用:HttpClient WebClient 文档 ...

  8. 如何选择 WebClient,HttpClient,HttpWebRequest

    当我们在用 .NET 调用 RestAPI 时通常有三种选择,分别为:WebClient, HttpWebRequest,HttpClient,这篇文章我们将会讨论如何使用这三种方式去调用 RestA ...

  9. C# Webclient 和 Httpclient如何通过iis authentication 身份验证。

    前言: 该博客产生的背景是客户那边有部署网站的方法是iis windows authentication身份验证,而系统中使用Webclient来调用别的系统的方法.在此情况下,原本可以使用的功能,都 ...

随机推荐

  1. jquery+css3实现3d滚动旋转

    最近有个漂亮妹子一直在向我推销潭州的视频,BUT我以前就看了一次觉得挺水的,就对那个妹子表示吾孤高.你们那玩意没意义的很弱.然后那个漂亮妹子锲而不舍的对我发链接,这个效果会吗,这个幻灯会写吗...我也 ...

  2. fullPage 全屏滚动【上下滚动】效果

    由于个人能力,研究了两天,终于写出来了. 又花了一天的时间成功的将30多行脚本,改成了70多行,我也是够有心的了. 那么接下来就是我制作这个效果的全部过程. 那一年我十七,她十八,在那个夏天里,我们, ...

  3. WCF 已知类型和泛型解析程序 KnownType

    数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...

  4. [译]Mongoose指南 - Model

    编译你的第一个model var xxSchema = new Schema({name: 'string', size: 'string'}); var Tank = mongoose.model( ...

  5. COCOS2D 释放资源的最佳时机

    有场景A跟场景B,场景A是当前场景,场景B是将要替换的新场景. 那么A场景的资源释放最佳时机是在什么时候呢? 这是释放资源的代码(注意要按这个顺序释放): 1 2 3 4 CCAnimationCac ...

  6. GridView控件隐藏列

    GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...

  7. 资源URL地址记录

    1. 如何搭建VPN服务器 http://www.360doc.com/content/11/0217/11/3084932_93749130.shtml http://www.softxp.net/ ...

  8. 北京程序员 VS 硅谷程序员(转)

    夫妻双码农,北京 or 硅谷,值得吗? http://mp.weixin.qq.com/s?__biz=MzA5MzE4MjgyMw==&mid=401228574&idx=1& ...

  9. 使用xp光盘修复系统的方法步骤

    对于使用Windows XP系统的朋友来说,当系统出现崩溃或者系统使用时出现一些莫名其妙的错误时,你采用什么方法解决呢?一般都是采用重装系统或者使用Ghost恢复等. 但是使用这些方法各有缺陷,比如重 ...

  10. ios 第三方登录

    ios 第三方登录通常的做法是, 1.登录后,获得第三方的uid,调用服务器登录接口,如服务器返回无此用户,转2,有此用户,转3. 2.服务器返回无此用户,需完善用户资料,如果完成,则在服务器创建与第 ...