我想在c#中编写一个Web服务,从流中接收多个文件,将它们读入不同的文件,然后对这些文件执行一些询问(例如,我需要发送的文件是XML): / p>
这是我到目前为止所做的:
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文件?
提前致谢....