c# Post 通过HttpWebRequest 网页通信

来源:互联网 发布:403 forbidden nginx 编辑:程序博客网 时间:2024/06/17 13:17
  1. /// <summary>
  2.         /// 发送消息到服务器
  3.         /// </summary>
  4.         /// <param name="xmldata">需要发送的消息</param>
  5.         /// <param name="url">服务器地址</param>
  6.         /// <returns>服务器返回的消息</returns>
  7.         public static string SendByPOST(string xmldata, string url)
  8.         {
  9.             string sXmlMessage = xmldata;
  10.             string DsmpUrl = url;
  11.             Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
  12.             byte[] arrB = encode.GetBytes(sXmlMessage);
  13.             HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(DsmpUrl);
  14.             myReq.Method = "POST" ;
  15.             myReq.ContentType = "application/x-www-form-urlencoded";
  16.             myReq.ContentLength = arrB.Length;
  17.             Stream outStream = myReq.GetRequestStream();            
  18.             outStream.Write(arrB, 0, arrB.Length);
  19.             outStream.Close();
  20.             WebResponse myResp = myReq.GetResponse();
  21.             Stream ReceiveStream = myResp.GetResponseStream();                
  22.             StreamReader readStream = new StreamReader( ReceiveStream, encode);
  23.             Char[] read = new Char[256];
  24.             int count = readStream.Read(read, 0, 256);
  25.             string str = null;
  26.             while (count > 0) 
  27.             {
  28.                 str += new String(read, 0, count);
  29.                 count = readStream.Read(read, 0, 256);
  30.             } 
  31.             readStream.Close();
  32.             myResp.Close();
  33.             return str;
  34.         }
原创粉丝点击