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);