httpPost 方式发送请求 不带参数

         /// <summary>
/// 没有参数的post请求
/// </summary>
public void HttpPostNoParam()
{ 
string Url = "请求地址";
HttpWebRequest request = WebRequest.CreateHttp(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Close();
myRequestStream.Close();
       
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}

httpPost 方式发送请求 带参数

           /// <summary>
/// 带有参数的post请求
       /// UserID、TransFlow 参数名
/// </summary>
public void HttpPostParam()
{  string Url = "请求地址";
HttpWebRequest request = WebRequest.CreateHttp(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string param = "UserID=2098&TransFlow=2";
byte[] by;
by = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = by.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myRequestStream.Write(by, , by.Length);
myStreamWriter.Close();
myRequestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}

httpGet 方式发送请求

         /// <summary>
/// Get方式
/// </summary>
public void HttpGet()
{
string url = "请求地址?ID=2";
HttpWebRequest request = WebRequest.CreateHttp(url);
request.Method = "Get";
request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream read = response.GetResponseStream();
StreamReader sm = new StreamReader(read, System.Text.Encoding.UTF8);
string readString = sm.ReadToEnd(); }
05-04 00:11