public string SendHttpWebRequest(bool isPost, string sendData, string requestURL)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(sendData);
//制备web请求
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestURL);
//设置传值方式
if (isPost)
{
myRequest.Method = "POST";
}
else
{
myRequest.Method = "GET";
}
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
if (isPost)
{
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}
//获取响应,调用HttpWebRequest.GetResponse()方法,把HTTP响应的数据流 (stream)绑定到StreamReader对象
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
//使用ReadToEnd()方法把整个HTTP响应作为一个字符串取回(ReadLine()方法逐行取回HTTP响应的内容)
return reader.ReadToEnd();
}