2

我想在 C# 中编写一个 Web 服务,它从流中接收多个文件,将它们读入不同的文件,然后对这些文件执行一些询问(例如,我需要发送的文件是 XML):

这是我到目前为止所做的:

1)通过以下方法发送文件,这将创建一个请求并将其发布到 Web 服务:

public void postMultipleFiles()
    {
        string url = "<the url of the web service>";
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;
        httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
        Stream memStream = new MemoryStream();
        string[] files = { @"<file1 location>", @"<file2 location>" };
        /*AJ*/
        foreach (string file in files)
        {
            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();
        }
        httpWebRequest.ContentLength = memStream.Length;
        Stream requestStream = httpWebRequest.GetRequestStream();
        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer, 0, tempBuffer.Length);
        requestStream.Close();
        try
        {
            WebResponse webResponse = httpWebRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            response.InnerHtml = reader.ReadToEnd();
            //Response.Write(reader.ReadToEnd());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        httpWebRequest = null;
    }

2)读取webservice中的流如下:

[WebMethod]
    public string MultiFilePost()
    {
        string xmls = "";
        if (HttpContext.Current.Request.InputStream != null)
        { 
            StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
            xmls = stream.ReadToEnd(); // added to view content of input stream
            xmls = xmls.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
        }
        return xmls;
    }

但这会将文件读入一个字符串,一个接一个地附加。

我如何设法将它放入两个不同的 XML 文件中?

提前致谢....

4

0 回答 0