阅读和保存帖子数据

时间:2016-04-28 13:11:01

标签: c# post listener file-transfer

我被指派接管某人的职位,但我真的不知道C#。有一个服务器(192.268。等等)将数据发布到一个站点(未知站点,比如说bleh.com)

这就是发布代码段的样子:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
  "https://server-ip-here/postlistener?filename=filename.zlib");

req.UseNagleAlgorithm = true;
req.AllowWriteStreamBuffering = true;
req.Method = "POST";
req.Accept = "application/xml";
req.ServicePoint.Expect100Continue = false;
System.Net.ServicePointManager.CheckCertificateRevocationList = false;
req.Proxy = new WebProxy();

filename = "filename.dat";
byte[] postData = File.ReadAllBytes(filename);
Stream stream = req.GetRequestStream();
stream.Write(postData, 0, postData.Length);
stream.Flush();
stream.Close();

req.BeginGetResponse(new AsyncCallback(responseHandler), this);

我相信我得到了这种形式的帖子请求

  

www.blah.com/upload?filename=file_1234_12389126495129847980.zlib

我不确定如何监听帖子请求,然后从中获取数据并将其保存为文件。

目前我试过这个:

 private void Form1_Load(object sender, EventArgs e)
 {
     listener = new HttpListener();
     // listener.Prefixes.Add("http://localhost:8000/");
     listener.Prefixes.Add("http://127.0.0.1:8000/");

     listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;

     listener.Start();
     listenThread1 = new Thread(new ParameterizedThreadStart(startlistener));
     listenThread1.Start();
 }

 private void startlistener(object s)
 {
     while (true)
     {               
         // blocks until a client has connected to the server
         ProcessRequest();
     }
 }

 private void ProcessRequest()
 {
     var result = listener.BeginGetContext(ListenerCallback, listener);
     result.AsyncWaitHandle.WaitOne();
 }

 private void ListenerCallback(IAsyncResult result)
 {
     var context = listener.EndGetContext(result);
     Thread.Sleep(1000);

     var data_text = new StreamReader(
         context.Request.InputStream,
         context.Request.ContentEncoding).ReadToEnd();

     var cleaned_data = System.Web.HttpUtility.UrlDecode(data_text);

     context.Response.StatusCode = 200;
     context.Response.StatusDescription = "OK";

     MessageBox.Show(cleaned_data);
     context.Response.Close();
 }

在本地主机上监听(一旦我们确定它将会是什么,就会对网站的子本地主机进行监听)。

不知道怎么抓住这个帖子,现在我只能听它。理想情况下,我也只想接受来自特定IP地址的帖子。

我是如何抓取帖子数据(将是二进制)并将其保存为文件的任何想法?

0 个答案:

没有答案