Exe摧毁自己

时间:2014-07-28 06:05:20

标签: c# crash exe

我用C#创建了一个控制台应用程序(exe)。应用程序用于向服务器发送一些http请求。它将json发送到调用服务器的API以执行某些工作。我将cookie保存在环境变量中。

我们注意到这个exe有些奇怪的行为。有些序列(我们无法找出这是什么序列)的API调用(api由命令行参数调用),exe会自行销毁。它的大小从15MB开始变为0KB。我以前从未见过这种行为。你知道它会是什么吗?

这是创建http请求的代码的主要部分,以及其他上传文件...

 static public JObject UploadFile(string fileName)
    {


        HttpWebRequest requestToServer = (HttpWebRequest)WebRequest.Create(serverDomain + directory + "/File");


        string boundaryString = "----SomeRandomText";
        requestToServer.AllowWriteStreamBuffering = false;
        requestToServer.Method = WebRequestMethods.Http.Post;
        requestToServer.ContentType = "multipart/form-data; boundary=" + boundaryString;
        requestToServer.KeepAlive = false;
        if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
        {

            CookieContainer cookies = new CookieContainer();
            cookies.Add(new Uri(serverDomain), new Cookie("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString()));

            requestToServer.CookieContainer = cookies;
        }

        ASCIIEncoding ascii = new ASCIIEncoding();
        string boundaryStringLine = "\r\n--" + boundaryString + "\r\n";
        byte[] boundaryStringLineBytes = ascii.GetBytes(boundaryStringLine);

        string lastBoundaryStringLine = "\r\n--" + boundaryString + "--\r\n";
        byte[] lastBoundaryStringLineBytes = ascii.GetBytes(lastBoundaryStringLine);


        string myFileDescriptionContentDisposition = String.Format(
            "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}",
            "myFileDescription",
            "A sample file description");
        byte[] myFileDescriptionContentDispositionBytes = ascii.GetBytes(myFileDescriptionContentDisposition);

        string fileUrl = fileName;

        string myFileContentDisposition = String.Format(
            "Content-Disposition: form-data;name=\"{0}\"; " + "filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n",
            "myFile", Path.GetFileName(fileUrl), Path.GetExtension(fileUrl));
        byte[] myFileContentDispositionBytes = ascii.GetBytes(myFileContentDisposition);

        FileInfo fileInfo = new FileInfo(fileUrl);


        long totalRequestBodySize = boundaryStringLineBytes.Length * 2 + lastBoundaryStringLineBytes.Length + myFileDescriptionContentDispositionBytes.Length + myFileContentDispositionBytes.Length + fileInfo.Length;

        requestToServer.ContentLength = totalRequestBodySize;

        using (Stream s = requestToServer.GetRequestStream())
        {

            s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
            s.Write(myFileDescriptionContentDispositionBytes, 0,
            myFileDescriptionContentDisposition.Length);


            s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
            s.Write(myFileContentDispositionBytes, 0,
            myFileContentDispositionBytes.Length);


            FileStream fileStream = new FileStream(fileUrl, FileMode.Open,
            FileAccess.Read);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                s.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();


            s.Write(lastBoundaryStringLineBytes, 0, lastBoundaryStringLineBytes.Length);
        }

        WebResponse response = requestToServer.GetResponse();
        StreamReader responseReader = new StreamReader(response.GetResponseStream());
        string replyFromServer = responseReader.ReadToEnd();
        JObject jsonResult = JObject.Parse(replyFromServer);
        //CreateErrorLog(replyFromServer);
        CreateResponse(replyFromServer);
        return jsonResult;
    }
    static public JObject request(string json)
    {



        var httpWebRequest = (HttpWebRequest)WebRequest.Create(serverDomain + directory + "/json.rpc");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";

        httpWebRequest.CookieContainer = new CookieContainer();
        if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
        {

            CookieContainer cookies = new CookieContainer();
            cookies.Add(new Uri(serverDomain), new Cookie("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString()));

            httpWebRequest.CookieContainer = cookies;
            httpWebRequest.Headers.Add("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString());
        }
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) == null)
            {
                foreach (Cookie cook in httpResponse.Cookies)
                {
                    Environment.SetEnvironmentVariable(cook.Name, cook.Value, EnvironmentVariableTarget.User);
                }
            }
            else if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
            {
                foreach (Cookie cook in httpResponse.Cookies)
                {
                    if (cook.Name == "dopinus.e2.session" && cook.Value != Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User))
                    {
                        Environment.SetEnvironmentVariable(cook.Name, cook.Value, EnvironmentVariableTarget.User);

                    }
                }

            }

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();

                JObject jsonResult = JObject.Parse(result);
                //CreateErrorLog(result);
                CreateResponse(result);
                return jsonResult;
            }
        }
    }

0 个答案:

没有答案