使用MemoryStream通过WebClient Post发送XML

时间:2011-10-27 18:26:09

标签: c# asp.net xml webclient memorystream

我正在尝试发送我在内存流中创建的XML文件。当我尝试使用UploadFile并获得无效参数的错误时,会出现此问题。我的问题是,无论如何我可以使用我用WebClient.UploadFile创建的内存流来发送文件,还是我需要以其他方式执行此操作?

string firstname = Request.Headers["firstname"].ToString();
string lastname = Request.Headers["lastname"].ToString();
string organization = Request.Headers["organization"].ToString();
string phone = Request.Headers["phone"].ToString();

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
MemoryStream ms = new MemoryStream();
using (XmlWriter xml = XmlWriter.Create(ms, settings))
{
    xml.WriteStartDocument();
    xml.WriteStartElement("List");
    xml.WriteWhitespace("\n");

    xml.WriteStartElement("Employee");
    {
        xml.WriteElementString("First", firstname);
        xml.WriteElementString("Last", lastname);
        xml.WriteWhitespace("\n");
        xml.WriteElementString("Organization", organization);
        xml.WriteElementString("Phone", phone);
    }

    xml.WriteEndElement();
    xml.WriteEndDocument();
}

WebClient client = new WebClient();

client.UploadFile("http://test.com/test.aspx", ms);

3 个答案:

答案 0 :(得分:9)

编辑:2013年编辑,因为当我最初回答时看起来完全错了......

选项:

  • MemoryStream获取字节数组并使用UploadData

    client.UploadData("http://test.com/test.aspx", ms.ToArray());
    
  • 改为使用OpenWrite,并写入返回的流

    using (var stream = client.OpenWrite("http://test.com/test.aspx"))
    {
        using (XmlWriter xml = XmlWriter.Create(stream, settings))
        {
            ...
        }
    }
    
  • 将数据写入文件,然后使用UploadFile

    using (XmlWriter xml = XmlWriter.Create("tmp.xml", settings))
    {
        ...
    }
    client.UploadFile("http://test.com/test.aspx", "tmp.xml");
    // Delete temporary file afterwards
    

我个人会采用前两种方法之一,以避免临时文件。

答案 1 :(得分:6)

根据您的Invalid Arguments

我认为WebClient.UploadFile上的任何方法都不支持MemoryStream。将您的MemoryStream转换为String并使用WebClient.UploadString

答案 2 :(得分:0)

我正在努力做同样的事情。我尝试过使用几种可能的解决方案,但没有成功。

当我尝试使用以下方法时,api获取请求,因为字符串文件名具有正确的值,但请求对象的内容为空...

using (FileStream src = System.IO.File.Open(@"C:\XML\OrderXML.txt", FileMode.Open))
            {
                src.CopyTo(ms);
            }

            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = "text/xml";
                wc.Headers[HttpRequestHeader.AcceptEncoding] = "Encoding.UTF8";
                //wc.Headers[HttpRequestHeader.ContentLength] = ms.Length.ToString();                    
                wc.UploadString(sURL, ms.ToString());
            }

我的RESTful API MVC控制器:

 [System.Web.Http.HttpPost]
    public async void CreateJob(string filename)
    {
        string success = "false";
        try
        {
            //XDocument doc = XDocument.Load
            XmlSerializer xmls = new XmlSerializer(typeof(PdnaXmlParse));
            Stream reqStream = await Request.Content.ReadAsStreamAsync();
            PdnaXmlParse res = (PdnaXmlParse)xmls.Deserialize(new XmlTextReader(reqStream));
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
}