1.C# 调用 MVC Controller 的方法,文字,图片当参数。
2.http方法的参数只能是字符串,图片以byte[]二进制 传递。
3.Newtonsoft.Json.JsonConvert.SerializeObject 会自动将byte[] 转为 base64(网络编码格式)的字符串。
------------------------------------------------------- A服务端
public string SetMouseAlarm(string dbname, string pid, string remarks, byte[] ImgBytes) { try { string sql = "INSERT INTO dbo.T_AlarmInfo VALUES(" + pid + ", NULL, NULL, NULL, '老鼠', 1, NULL, 0, 1, '一般', GETDATE(), '', NULL, 0, '" + remarks + "');SELECT MAX(ID) FROM dbo.T_AlarmInfo;"; string id = DBHelper.ExecuteNonQurey1(dbname, sql, CommandType.Text, null).ToString(); //////////////// System.IO.MemoryStream ms = new System.IO.MemoryStream(ImgBytes); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); string imagePath = Server.MapPath("~/ShangChuan/LaoShuImages"); //保存到磁盘文件 if (!System.IO.Directory.Exists(imagePath)) System.IO.Directory.CreateDirectory(imagePath); //img.Save(System.IO.Path.Combine(imagePath, DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); img.Save(System.IO.Path.Combine(imagePath, id + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); ms.Dispose(); return "1"; } catch (Exception e) { return e.Message; } }View Code
B---------------客户端调用
1.客户端http封装类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace WebApplication1.HelpClass { public class HttpClient { /// <summary> /// Seivice URL /// </summary> public string Url { get; set; } /// <summary> /// 内容,body,fields参数 /// </summary> public string Data { get; set; } /// <summary> /// Cookie,保证登录后,所有访问持有一个Cookie; /// </summary> static CookieContainer Cookie = new CookieContainer(); /// <summary> /// Seivice Method ,post,get,delete,put,等等,支持大小写 /// </summary> public string Method { get; set; } /// <summary> /// HTTP访问 /// </summary> public string AsyncRequest() { HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest; httpRequest.Method = Method; httpRequest.ContentType = "application/json"; httpRequest.CookieContainer = Cookie; httpRequest.Timeout = 1000 * 60 * 10;//10min using (Stream reqStream = httpRequest.GetRequestStream()) { var bytes = UnicodeEncoding.UTF8.GetBytes(Data); reqStream.Write(bytes, 0, bytes.Length); reqStream.Flush(); } using (var repStream = httpRequest.GetResponse().GetResponseStream()) { using (var reader = new StreamReader(repStream)) { return ValidateResult(reader.ReadToEnd()); } } } private static string ValidateResult(string responseText) { if (responseText.StartsWith("response_error:")) { return responseText.TrimStart("response_error:".ToCharArray()); } return responseText; } } public class UserInfo { public string dbname { get; set; } public string pid { get; set; } public string remarks { get; set; } public byte[] ImgBytes { get; set; } } }View Code
2.调用方法
public void aaaa() { HelpClass.HttpClient httpClient = new HelpClass.HttpClient(); httpClient.Url = "http://172.16.6.133:8067/Alarm/SetMouseAlarm"; httpClient.Method = "post"; HelpClass.UserInfo userInfo = new HelpClass.UserInfo(); userInfo.dbname = "DB_YWSystem_cqly5"; userInfo.pid = "1"; userInfo.remarks = "老鼠报警3"; userInfo.ImgBytes = GetPictureData(); httpClient.Data = Newtonsoft.Json.JsonConvert.SerializeObject(userInfo); var iResult = httpClient.AsyncRequest(); //var iResult = Newtonsoft.Json.Linq.JObject.Parse(); } //图片转成二进制 public byte[] GetPictureData() { string path = Server.MapPath("~/ShangChuan/LaoShuImages/AA.jpg"); System.IO.FileStream a = new System.IO.FileStream(path, System.IO.FileMode.Open); byte[] byData = new byte[a.Length]; a.Read(byData, 0, byData.Length); a.Close(); return byData; //SetMouseAlarm("DB_YWSystem_cqly5", "1", "发现老鼠报警...", byData);http://172.16.6.133:8067/Alarm/SetMouseAlarm }View Code
标签:string,get,C#,System,Controller,MVC,IO,using,public From: https://www.cnblogs.com/Evan-Pei/p/16914883.html