欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#請(qǐng)求http向網(wǎng)頁(yè)發(fā)送接收數(shù)據(jù)的方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例為大家分享了C#請(qǐng)求http向網(wǎng)頁(yè)發(fā)送數(shù)據(jù)、網(wǎng)頁(yè)接收,供大家參考,具體內(nèi)容如下

首先,我們需要的是什么東西?

用POST方式請(qǐng)求http,給網(wǎng)頁(yè)傳輸數(shù)據(jù),網(wǎng)頁(yè)接收到數(shù)據(jù)之后,把數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中。

1.首先請(qǐng)求http,建立連接,把轉(zhuǎn)碼過(guò)的數(shù)據(jù)傳輸過(guò)去

2.網(wǎng)頁(yè)接收數(shù)據(jù),在轉(zhuǎn)碼之后存儲(chǔ)到數(shù)據(jù)庫(kù)

3.網(wǎng)頁(yè)返回一個(gè)東西給傳輸方,表示我們已經(jīng)接收到數(shù)據(jù)了

同樣,我們請(qǐng)求http也是用的控制臺(tái)模擬的

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">請(qǐng)求后臺(tái)地址</param> 
    /// <param name="content">Post提交數(shù)據(jù)內(nèi)容(utf-8編碼的)</param> 
    /// <returns>結(jié)果</returns> 
    public static string Post(string url, string content)
    {
      //申明一個(gè)容器result接收數(shù)據(jù)
      string result = "";
      //首先創(chuàng)建一個(gè)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();
      }

      //申明一個(gè)容器resp接收返回?cái)?shù)據(jù)
      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
      Stream stream = resp.GetResponseStream();
      //獲取響應(yīng)內(nèi)容 
      using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
      {
        result = reader.ReadToEnd();
      }
      return result;
    }

然后,在Controller里面有個(gè)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ù)添加進(jìn)去
      service.AddCatagorys(result);
      //再調(diào)用GetCatas方法,獲取到分類(lèi)列表
      List<Catagory> list = service.GetCatagories();
      //找到分類(lèi)列表最后一個(gè)分類(lèi),也就是剛剛添加的分類(lèi)
      Catagory catagory = list[list.Count - 1];
      //返回Json
      //return Json(catagory)

      //返回一個(gè)ID,Content()里面是string類(lèi)型,所以要把int轉(zhuǎn)為string類(lèi)型
      return Content(catagory.ID.ToString());
    }

這里其實(shí)相當(dāng)于兩個(gè)人打電話,你在跟我打電話的時(shí)候,按理來(lái)說(shuō)不會(huì)再跟其他人打電話唄。

所以這里return Content(catagory.ID.ToString());表示將返回的ID再返還給控制臺(tái),也就是傳輸方,讓傳輸方知道我們接收到你傳輸過(guò)來(lái)的數(shù)據(jù),并且把它保存到數(shù)據(jù)庫(kù)里面了。

注:此篇隨筆只供參考使用,而且也有很多小瑕疵,最主要的不是代碼,邏輯才是最重要的。

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有