C# 傳送POST請求

sky_cat發表於2017-06-15
            HttpWebRequest req = WebRequest.Create("http://test.do.do") as HttpWebRequest;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = Encoding.UTF8.GetByteCount("username=admin");
            string rr = "username=admin";
            using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
            sw.Write("username=admin");
            HttpWebResponse res = req.GetResponse() as HttpWebResponse;
            string responseString = "";
            using (Stream responseStream = res.GetResponseStream())
            {
                using (StreamReader srResponse = new StreamReader(responseStream))
                {
                    //返回的json格式的資料
                    responseString = srResponse.ReadToEnd();
                }
            }
            string aa = responseString;

相關文章