C#請求http向網(wǎng)頁發(fā)送接收數(shù)據(jù)的方法
本文實例為大家分享了C#請求http向網(wǎng)頁發(fā)送數(shù)據(jù)、網(wǎng)頁接收,供大家參考,具體內(nèi)容如下
首先,我們需要的是什么東西?
用POST方式請求http,給網(wǎng)頁傳輸數(shù)據(jù),網(wǎng)頁接收到數(shù)據(jù)之后,把數(shù)據(jù)存儲到數(shù)據(jù)庫中。
1.首先請求http,建立連接,把轉(zhuǎn)碼過的數(shù)據(jù)傳輸過去
2.網(wǎng)頁接收數(shù)據(jù),在轉(zhuǎn)碼之后存儲到數(shù)據(jù)庫
3.網(wǎng)頁返回一個東西給傳輸方,表示我們已經(jīng)接收到數(shù)據(jù)了
同樣,我們請求http也是用的控制臺模擬的
static void Main(string[] args) { string result = Post("http://localhost:5534/Home/ToUrl", "家庭"); Console.WriteLine(result); Console.ReadKey(); } /// <summary> /// 指定Post地址使用Get 方式獲取全部字符串 /// </summary> /// <param name="url">請求后臺地址</param> /// <param name="content">Post提交數(shù)據(jù)內(nèi)容(utf-8編碼的)</param> /// <returns>結果</returns> public static string Post(string url, string content) { //申明一個容器result接收數(shù)據(jù) string result = ""; //首先創(chuàng)建一個HttpWebRequest,申明傳輸方式POST HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; //添加POST參數(shù) byte[] data = Encoding.UTF8.GetBytes(content); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } //申明一個容器resp接收返回數(shù)據(jù) HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //獲取響應內(nèi)容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }
然后,在Controller里面有個ToUrl用于接收數(shù)據(jù)
public ActionResult ToUrl() { string result = ""; string jsonStr = "", line; try { Stream streamResponse = Request.InputStream; StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8); while ((line = streamRead.ReadLine()) != null) { jsonStr += line; } streamResponse.Close(); streamRead.Close(); result = jsonStr; } catch (Exception ex) { result = "msg-數(shù)據(jù)發(fā)布(In)異常:" + ex.Message; } Service service = new Service(); //調(diào)用AddCatagorys方法,把數(shù)據(jù)添加進去 service.AddCatagorys(result); //再調(diào)用GetCatas方法,獲取到分類列表 List<Catagory> list = service.GetCatagories(); //找到分類列表最后一個分類,也就是剛剛添加的分類 Catagory catagory = list[list.Count - 1]; //返回Json //return Json(catagory) //返回一個ID,Content()里面是string類型,所以要把int轉(zhuǎn)為string類型 return Content(catagory.ID.ToString()); }
這里其實相當于兩個人打電話,你在跟我打電話的時候,按理來說不會再跟其他人打電話唄。
所以這里return Content(catagory.ID.ToString());表示將返回的ID再返還給控制臺,也就是傳輸方,讓傳輸方知道我們接收到你傳輸過來的數(shù)據(jù),并且把它保存到數(shù)據(jù)庫里面了。
注:此篇隨筆只供參考使用,而且也有很多小瑕疵,最主要的不是代碼,邏輯才是最重要的。
相關文章
經(jīng)典排序算法之冒泡排序(Bubble sort)代碼
這篇文章主要介紹了經(jīng)典排序算法之冒泡排序(Bubble sort)代碼的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06C#利用Spire.Pdf包實現(xiàn)為PDF添加數(shù)字簽名
Spire.PDF for .NET 是一款專業(yè)的基于.NET平臺的PDF文檔控制組件。它能夠讓開發(fā)人員在不使用Adobe Acrobat和其他外部控件的情況下,運用.NET 應用程序創(chuàng)建,閱讀,編寫和操縱PDF 文檔。本文將利用其實現(xiàn)添加數(shù)字簽名,需要的可以參考一下2022-08-08C# DataGridView綁定數(shù)據(jù)源的方法
這篇文章主要為大家詳細介紹了C# DataGridView綁定數(shù)據(jù)源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09利用多線程句柄設置鼠標忙碌狀態(tài)的實現(xiàn)方法
怎樣利用多線程句柄設置鼠標忙碌狀態(tài)呢?下面小編就為大家介紹一下具體的實現(xiàn)方法吧!需要的朋友可以過來參考下2013-08-08