将控制台输出保存到文件中的查询

时间:2015-12-31 09:08:04

标签: prolog

我使用Swi-prolog for windows并希望将答案保存到文件中的查询中。举一个简单的例子,假设: 所述' input.pl' file有这些谓词:

public static async Task<string> PostMultiPartForm(string url, byte[] file, string paramName, string contentType, Dictionary<String, string> nvc,
        string cookie)
    {
        // log.Debug(string.Format("Uploading {0} to {1}", file, url));
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        wr.Headers ["Cookie"] = cookie;
        //wr.KeepAlive = true;
        //wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

        Stream rs =  await wr.GetRequestStreamAsync();

        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
        foreach (string key in nvc.Keys)
        {
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            rs.Write(formitembytes, 0, formitembytes.Length);
        }
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string header = string.Format(headerTemplate, paramName, file, contentType);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length);

        rs.Write(file,0, file.Length);


        byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        //rs.Close();
        string responseString = String.Empty;
        WebResponse wresp = null;
        try
        {
            wresp = await wr.GetResponseAsync();
            Stream respStream = wresp.GetResponseStream();
            StreamReader respReader = new StreamReader(respStream);
            responseString = respReader.ReadToEnd();
            //log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
        }
        catch (Exception ex)
        {
            //log.Error("Error uploading file", ex);
            if (wresp != null)
            {
                //wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
        return responseString;

    }

所以,通过电话&#39;回答&#39;查询,我想保存&#39;是&#39;在output.txt文件中。 我该怎么做?

0 个答案:

没有答案