无网不进  
软硬件开发

GET请求

 

复制代码
          string postData = ""; // 要发放的数据 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/");
            objWebRequest.Method = "GET";
            objWebRequest.ContentType = "application/x-www-form-urlencoded";
            objWebRequest.ContentLength = byteArray.Length;

            HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string textResponse = sr.ReadToEnd(); // 返回的数据
            Console.WriteLine(textResponse);
复制代码

 

POST请求

 

复制代码
          string postData = ""; // 要发放的数据 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("https://www.baidu.com/");
            objWebRequest.Method = "POST";
            objWebRequest.ContentType = "application/x-www-form-urlencoded";
            objWebRequest.ContentLength = byteArray.Length;
            Stream newStream = objWebRequest.GetRequestStream();
            // Send the data. 
            newStream.Write(byteArray, 0, byteArray.Length); //写入参数 
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string textResponse = sr.ReadToEnd(); // 返回的数据
            Console.WriteLine(textResponse);
posted on 2018-05-28 18:33  无网不进  阅读(269)  评论(0编辑  收藏  举报