0

我目前正在尝试将 xml 文件发布到 Web 服务。

这是我当前的代码

 var tempFileLocation = @"c:\Temp\";             

            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(item);
            xdoc.Save(tempFileLocation + "\\pcf.xml");

            var url = "https://Siteaddress/xml.aspx";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byte1 = encoding.GetBytes(xdoc.InnerXml);
            HttpWebRequest submissionRequest = (HttpWebRequest)WebRequest.Create(url);
            submissionRequest.KeepAlive = true;
            submissionRequest.Method = "POST";
            submissionRequest.ContentType = "multipart/form-data";
            submissionRequest.ContentLength = byte1.Length;
            submissionRequest.Timeout = 10000;
            submissionRequest.Headers.Add("Accept-Encoding", "gzip,deflate"); 

            Stream requestStream = submissionRequest.GetRequestStream();
            requestStream.Write(byte1, 0, byte1.Length);
            requestStream.Close();

            XmlDocument responseXMLDocument = new XmlDocument();

            HttpWebResponse submissionAcknowledgement = (HttpWebResponse)submissionRequest.GetResponse();

            if (submissionAcknowledgement.StatusCode == HttpStatusCode.OK)
            {
                responseXMLDocument.Load(submissionAcknowledgement.GetResponseStream());

            }

然后我收到以下消息。

错误的 Request.Form[pcfxml] - 这应该作为 POST 附加

我应该如何将 xml 文件作为附件发布?

4

2 回答 2

0

似乎缺少边界。需要为所有分段上传设置边界...

boundary = GetBoundary();

request.ContentType = "multipart/form-data; boundary=" + boundary;

和边界应该如下所示。请注意,每当您将字节写入请求流时,请确保您已提及边界。

private static string GetBoundary()
{
    return "--------------------" + DateTime.Now.Ticks.ToString("x");
}
于 2013-07-23T09:33:12.773 回答
0

最终我找到了一个解决方案,我需要通过 WebClient 传递 NameValueCollection

var url = string.Format("https://Site-Address/xml.aspx");

        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("pcfxml", productSerialized);

        WebClient wc = new WebClient();
        var response = wc.UploadValues(url, nvc);

感谢 Aundy 的回复,我玩过边界。

于 2013-07-23T16:44:10.870 回答