1. 程式人生 > >後端伺服器請求

後端伺服器請求

設定 Uri

 Uri urlthree = new Uri("http://cloud.calmcar.com/data/api/vboxpush.action");

設定byte[] 陣列

  byte[] sssss = System.Text.Encoding.UTF8.GetBytes("fdsafdsafdsa");

另外新增一種byte[]與string 轉換(一般電腦程式都是按照byte[]儲存位元組,例如:{01,02,03,04,262})

string ssssssssss= System.Text.Encoding.UTF8.GetString(sssss);

我們一般常用的的前端 的Ajax 非同步請求

後端的請求多用於小程式,可以直接丟伺服器上執行,操作簡單

雖然後端比前端的請求難一些,不著急

我馬上附上後端請求,可以直接copy使用

 public static string Http(Uri uri, byte[] data = null)
        {
            string rtnVal = "";
            int tryTimes = 0;
        again:
            tryTimes++;
            try
            {
                HttpWebRequest webRequest 
= (HttpWebRequest)System.Net.WebRequest.Create(uri); webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded"; if (data != null) { webRequest.Method = "POST"; webRequest.ContentLength
= data.Length; Stream inputStream = webRequest.GetRequestStream(); inputStream.Write(data, 0, data.Length); inputStream.Close(); inputStream.Dispose(); inputStream = null; } HttpWebResponse webResp = (HttpWebResponse)webRequest.GetResponse(); using (Stream receiveStream = webResp.GetResponseStream()) { using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) { rtnVal = readStream.ReadToEnd(); } } } catch (Exception) { if (tryTimes < 1) goto again; } return rtnVal; } } } 
多組傳參 byte[] sssss = System.Text.Encoding.UTF8.GetBytes({\"Action\":\"GetCarMainInfo\",\"CarId\":\"1000707\"});