This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24858|Reply: 1

[Source] HttpListener receives the post request

[Copy link]
Posted on 12/17/2015 11:44:12 AM | | | |


  1. /// <summary>
  2.         /// HttpListener接收post请求
  3.         /// </summary>
  4.         /// <param name="request"></param>
  5.         /// <returns></returns>
  6.         private string PostInput(HttpListenerRequest request)
  7.         {
  8.             try
  9.             {
  10.                 System.IO.Stream s = request.InputStream;
  11.                 int count = 0;
  12.                 byte[] buffer = new byte[1024];
  13.                 StringBuilder builder = new StringBuilder();
  14.                 while ((count = s.Read(buffer, 0, 1024)) > 0)
  15.                 {
  16.                     builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
  17.                 }
  18.                 s.Flush();
  19.                 s.Close();
  20.                 s.Dispose();
  21.                 return builder.ToString();
  22.             }
  23.             catch (Exception ex)
  24.             { throw ex; }
  25.         }
Copy code






Previous:C# Image and Base64 encoded interconversion functions
Next:.net uses HttpListener to listen to content and uses threads to respond to users to prevent blocking
 Landlord| Posted on 12/17/2015 11:56:23 AM |
Attach the post method

  1. /// <summary>
  2.         /// POST请求与获取结果
  3.         /// </summary>
  4.         public static string HttpPost(string Url, string postDataStr)
  5.         {
  6.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  7.             request.Method = "POST";
  8.             request.ContentType = "application/x-www-form-urlencoded";
  9.             request.ContentLength = postDataStr.Length;
  10.             StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
  11.             writer.Write(postDataStr);
  12.             writer.Flush();
  13.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  14.             string encoding = response.ContentEncoding;
  15.             if (encoding == null || encoding.Length < 1)
  16.             {
  17.                 encoding = "UTF-8"; //默认编码
  18.             }
  19.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
  20.             string retString = reader.ReadToEnd();
  21.             return retString;
  22.         }
Copy code


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com