将带有post请求的xml发送到iis上的wcf服务

本文关键字:iis 上的 服务 wcf xml post 请求 | 更新日期: 2023-09-27 18:01:00

使用此链接解决

我正在开发一个带有rest模式的wcf服务。现在它位于IIS服务器上。

它有一个可以用post调用的方法,该方法必须接收xml数据。

我正试图发送这个xml(带有其他参数(,但我只收到了取消序列的错误:

in System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
in System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
in System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
in System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
in System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
in System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

这是请求:

retista=0019&hashedStrig=dkfjdkfjd&dati=<ArrayOfWrapClienti xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><WrapClienti><CODRETE>0018</CODRETE><CODICE>20685</CODICE><NOME>A.T.E.R. Azienda Territoriale</NOME><INDIRIZZO>PIAZZA POZZA</INDIRIZZO><CITTA>Verona</CITTA><CAP>37123</CAP><PROV>VR</PROV><CODICEFISCALE>00223640236</CODICEFISCALE><PIVA>223640236</PIVA><EMAIL/><ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE><STATO>FALSE</STATO></WrapClienti></ArrayOfWrapClienti>

这就是我要发送的xml:

<ArrayOfWrapClienti xmlns="http://schemas.datacontract.org/2004/07/aaa" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<WrapClienti>
<CODRETE>1111</CODRETE>
<CODICE>111</CODICE>
<NOME>AAA</NOME>
<INDIRIZZO>PIAZZA</INDIRIZZO>
<CITTA>AAA</CITTA>
<CAP>111</CAP>
<PROV>aa</PROV>
<CODICEFISCALE>111</CODICEFISCALE>
<PIVA>223611140236</PIVA>
<EMAIL/>
<ESPOSIZ_CONTABILE>1111</ESPOSIZ_CONTABILE>
<STATO>FALSE</STATO>
</WrapClienti>
</ArrayOfWrapClienti>

如果单击此处,您可以了解我的wcf是如何返回xml的。我想我必须将相同的xml与其他数据一起发送,但我收到了取消序列号的错误。

为什么?

如何将xml发送到我的wcf?在这个测试阶段,我使用这个。

如果我能给你其他有用的信息,请问我,我会把它们写在这里。

UPDATE:错误消息的文本表示在根级别的第1行,位置1存在无效数据。如果我放

<?xml version="1.0" encoding="utf-8" ?>

在根级别。

也许是缺少了一些标题

更新服务器端

这就是我的服务器wcf的配置方式:

    [OperationContract]
    [WebGet(UriTemplate = "cliente.xml?retista={codret}&cliente={codiceCliente}&H={hashedString}", ResponseFormat = WebMessageFormat.Xml)]
    List<WrapClienti> GetClienteXML(string codret, string codiceCliente, string hashedString);

这就是所谓的方法:

    public GenericResponse SetClientiXML(List<WrapClienti> clienti, string retista, string hashedString) 
    {
        var api = new API();
        return api.SetClienti(clienti, retista, hashedString);        
    }

正如您所看到的,我不管理任何xml/json反序列化阶段,它是由wcf例程管理的。例如,如果我发送一个json列表,它会将其转换为list。这也是我想看到的xml,但我在这个问题的顶部得到了错误。

另一个更新:我必须用xml发送3个参数,所以……这是我最后一次尝试,我认为它比以前的更正确。现在服务器给出空引用错误:

<?xml version="1.0" encoding="utf-8" ?>
<SetClientiXML
  xmlns="http://tempuri.org/">
  <XMLRequest>
    <ArrayOfWrapClienti
      xmlns="http://schemas.datacontract.org/2004/07/aaa"
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <WrapClienti>
        <CODRETE>111</CODRETE>
        <CODICE>111</CODICE>
        <NOME>aaa</NOME>
        <INDIRIZZO>aaa</INDIRIZZO>
        <CITTA>aaa</CITTA>
        <CAP>aaa</CAP>
        <PROV>aa</PROV>
        <CODICEFISCALE>111</CODICEFISCALE>
        <PIVA>2236401111236</PIVA>
        <EMAIL/>
        <ESPOSIZ_CONTABILE>111</ESPOSIZ_CONTABILE>
        <STATO>FALSE</STATO>
      </WrapClienti>
    </ArrayOfWrapClienti>
    <RETISTA>1111</RETISTA>
    <HASHEDSTRING>oklkokokokok</HASHEDSTRING>
  </XMLRequest>
</SetClientiXML>

我还创建了这个类:

[DataContract]
public class XMLRequest
{
    [DataMember]
    public List<WrapClienti> XML;
    [DataMember]
    public string RETISTA;
    [DataMember]
    public string HASHEDSTRING;
    public XMLRequest() { }
    public XMLRequest(List<WrapClienti> x, string r, string h)
    {
        this.HASHEDSTRING = h;
        this.XML = x;
        this.RETISTA = r;
    }
}

现在的portotype是:

public GenericResponse SetClientiXML(XMLRequest xr)

将带有post请求的xml发送到iis上的wcf服务

这行得通吗?

            string input =
                "<ArrayOfWrapClienti xmlns='"http://schemas.datacontract.org/2004/07/MultipayOnline'" xmlns:i='"http://www.w3.org/2001/XMLSchema-instance'">" +
                "<WrapClienti>" +
                "<CODRETE>0018</CODRETE>" +
                "<CODICE>20685</CODICE>" +
                "<NOME>A.T.E.R. Azienda Territoriale</NOME>" +
                "<INDIRIZZO>PIAZZA POZZA</INDIRIZZO>" +
                "<CITTA>Verona</CITTA>" +
                "<CAP>37123</CAP>" +
                "<PROV>VR</PROV>" +
                "<CODICEFISCALE>00223640236</CODICEFISCALE>" +
                "<PIVA>223640236</PIVA>" +
                "<EMAIL/>" +
                "<ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE>" +
                "<STATO>FALSE</STATO>" +
                "</WrapClienti>" +
                "</ArrayOfWrapClienti>";
            input = "<?xml version='"1.0'" encoding='"utf-8'"?>" + input;​

以下是用于序列化和反序列化xml 的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:'temp'test.xml";
        static void Main(string[] args)
        {
            SetClientiXML client = new SetClientiXML()
            {
                xmlRequest = new XMLRequest()
                {
                    arrayOfWrapClienti = new ArrayOfWrapClienti()
                    {
                        wrapClient = new List<WrapClient>() {
                            new WrapClient {
                                codrete = "0018",
                                codice = "20685",
                                nome = "A.T.E.R. Azienda Territoriale",
                                indirizzo = "PIAZZA POZZA",
                                citta = "Verona",
                                cap = "37123",
                                prov = "VR",
                                codiceFiscale = "00223640236",
                                piva = "223640236",
                                esposizContabile = "937,02",
                                stato = false
                            }
                        }
                    }
                },
                retista = "3303903",
                hashedString = "oklkokokokok"
            };
            XmlSerializer serializer = new XmlSerializer(typeof(SetClientiXML));
            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, client);
            writer.Flush();
            writer.Close();
            writer.Dispose();
            XmlSerializer xs = new XmlSerializer(typeof(SetClientiXML));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            SetClientiXML newClient = (SetClientiXML)xs.Deserialize(reader);
        }
    }
    [XmlRoot("SetClientiXML")]
    public class SetClientiXML
    {
        [XmlElement("XMLRequest")]
        public XMLRequest xmlRequest { get; set; }
        [XmlElement("RETISTA")]
        public string retista { get; set; }
        [XmlElement("HASHEDSTRING")]
        public string hashedString { get; set; }
    }
    [XmlRoot("XMLRequest")]
    public class XMLRequest
    {
        [XmlElement("ArrayOfWrapClienti")]
        public ArrayOfWrapClienti arrayOfWrapClienti { get; set; }
    }
    [XmlRoot("ArrayOfWrapClienti")]
    public class ArrayOfWrapClienti
    {
        [XmlElement("WrapClient")]
        public List<WrapClient> wrapClient { get; set; }
    }
    [XmlRoot("WrapClient")]
    public class WrapClient
    {
        [XmlElement("CODRETE")]
        public string codrete { get; set; }
        [XmlElement("CODICE")]
        public string codice { get; set; }
        [XmlElement("NOME")]
        public string nome { get; set; }
        [XmlElement("INDIRIZZO")]
        public string indirizzo { get; set; }
        [XmlElement("CITTA")]
        public string citta { get; set; }
        [XmlElement("CAP")]
        public string cap { get; set; }
        [XmlElement("PROV")]
        public string prov { get; set; }
        [XmlElement("CODICEFISCALE")]
        public string codiceFiscale { get; set; }
        [XmlElement("PIVA")]
        public string piva { get; set; }
        [XmlElement("EMAIL")]
        public string email { get; set; }
        [XmlElement("ESPOSIZ_CONTABILE")]
        public string esposizContabile { get; set; }
        [XmlElement("STATO")]
        public Boolean stato { get; set; }
    }
}
​