从表单发布下载文件

时间:2011-09-24 12:42:09

标签: c# .net

我有一个c#脚本可以做两件事

此脚本连接我的Web服务器以获取生成的密码,

然后建立一个连接,其中一个表格带有从我的网络服务器获取的密码,

问题是当表单发布时它会响应应用程序现在我需要运行这个应用程序我不在乎我是否必须保存exe然后运行它或者如果我可以从内存运行它

这是我的脚本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 responce = req.GetResponse();

       hasDownloaded = true;
  }

1 个答案:

答案 0 :(得分:3)

您可以将响应保存到文件中然后运行它(假设它当然是可执行文件):

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");