C#的遠程post接收與請求

            //接收
            Stream stream = HttpContext.Current.Request.InputStream;
            byte[] getByte = new byte[stream.Length];
            stream.Read(getByte, 0, getByte.Length);
            string postStr = Encoding.UTF8.GetString(getByte);

            //請求
            string url = "";
            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.Method = "post";
            webrequest.ContentType = "application/json;charset=UTF-8";
            byte[] postByte = Encoding.UTF8.GetBytes(postStr);
            webrequest.ContentLength = postByte.Length;
            Stream stream1 = webrequest.GetRequestStream();
            stream1.Write(postByte, 0, postByte.Length);
            stream1.Close();
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)webrequest.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;//返回遠程服務器報告回來的錯誤
            }
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string getStr = sr.ReadToEnd();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章