开发者

Download a file from a form post

I have a c# script that does 2 things

This script connects a my web server to get a pin number generated,

It then makes a connection where its post a form with that pin number it gets from my web server,

The problem is when the form is posted it responds with an application now i need to run this application i don't care if i have to save the exe then run it or if i can run it from memory

Here is my script sofar

 string[] responseSplit;
 bool connected = false;
 try
 {
       request = (HttpWebRequest)WebRequest.Create(API_url + "prams[]=");
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       connected = true;
  }
  catch(Exception e)
  {
                MessageBox.Show(API_url + "prams[]=");
  }
  if (!connected)
  {
         MessageBox.Show("Support Requires and Internet Connection.");
  }
  else
  {
        request = (HttpWebRequest)WebRequest.Create(API_url + "prams[]=");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream resStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(resStream);
        string responceString = reader.ReadToEnd();
        responseSplit = responceString.Split('\n');

        WebRequest req = WebRequest.Create("https://secure.logmeinrescue.com/Customer/Code.aspx");

        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";

        byte[] bytes = Encoding.ASCII.GetBytes("Code=" + responseSplit[1]);
        req.ContentLength = bytes.Length;

        Stream os = req.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
        os.Close();

       WebResponse re开发者_运维问答sponce = req.GetResponse();

       hasDownloaded = true;
  }


Well you could save the response into a file and then run it (assuming it's an executable of course):

using (var response = req.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var output = new FileStream("test.exe", FileMode.Create, FileAccess.Write))
{
    var buffer = new byte[2048]; // read in chunks of 2KB
    int bytesRead;
    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}
Process.Start("test.exe");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜