c# 後台GET、POST、PUT、DELETE傳輸發送json資料

來源:互聯網
上載者:User

標籤:get、post、   let   ati   res   pre   imu   tor   對象   try   

 

一、Get 方式傳輸

     //url為請求的網址,param參數為需要查詢的條件(服務端接收的參數,沒有則為null)        //返回該次請求的響應        public string HttpGet(string url, Dictionary<String, String> param)        {            if (param != null) //有參數的情況下,拼接url            {                url = url + "?";                foreach (var item in param)                {                    url = url + item.Key + "=" + item.Value + "&";                }                url = url.Substring(0, url.Length - 1);            }            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//建立請求            request.Method = "GET"; //要求方法為GET            HttpWebResponse res; //定義返回的response            try            {                res = (HttpWebResponse)request.GetResponse(); //此處發送了請求並獲得響應            }            catch (WebException ex)            {                res = (HttpWebResponse)ex.Response;            }            StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);            string content = sr.ReadToEnd(); //響應轉化為String字串            return content;        }

二、POST 方式傳輸

public static string HttpPost(string url, Dictionary<String, String> param)        {            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //建立請求            CookieContainer cookieContainer = new CookieContainer();            request.CookieContainer = cookieContainer;            request.AllowAutoRedirect = true;            //request.AllowReadStreamBuffering = true;            request.MaximumResponseHeadersLength = 1024;            request.Method = "POST"; //請求方式為post            request.AllowAutoRedirect = true;            request.MaximumResponseHeadersLength = 1024;            request.ContentType = "application/json";            JObject json = new JObject();            if (param.Count != 0) //將參數添加到json對象中            {                foreach (var item in param)                {                    json.Add(item.Key, item.Value);                }            }            string jsonstring = json.ToString();//獲得參數的json字串            byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);            Stream postStream = request.GetRequestStream();            postStream.Write(jsonbyte, 0, jsonbyte.Length);            postStream.Close();            //發送請求並擷取相應回應資料                   HttpWebResponse res;            try            {                res = (HttpWebResponse)request.GetResponse();            }            catch (WebException ex)            {                res = (HttpWebResponse)ex.Response;            }            StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);            string content = sr.ReadToEnd(); //獲得響應字串            return content;        }

其中PUT、DELETE方式跟上面基本相似。這裡就不再多說明

c# 後台GET、POST、PUT、DELETE傳輸發送json資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.