如何发送2 xml请求肥皂?

时间:2013-12-23 09:25:15

标签: c# .net xml soap

我有一个包含两个XML字符串的列表,这里是C#代码:

 string sb = "";
List<string> listGamme = new List<string>();
sb = ("<?xml....") ;//1st listGamme
listGamme.Add(sb);
sb = ("<?xml..."); //2nd listGamme
listGamme.Add(sb);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("urlofmy.asmx");
req.Headers.Add("SOAPAction", "URLSOAPACTION");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";


                using (Stream stm = req.GetRequestStream())
                {

                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                         foreach (string gamme in listGamme)
                            {

                                 stmw.Write(gamme);

                             }   

                     }

                }

    WebResponse response = req.GetResponse();
        StreamReader srreader = new StreamReader(response.GetResponseStream());

在de second中添加foreach使用给我错误400错误请求:),如果第一个 listGamme 或第二个 listGamme 是“注释掉”代码工作!,我的代码有什么问题??

1 个答案:

答案 0 :(得分:2)

在代码中应用循环以逐个发送请求。如果你的wsdl不允许,你不能发送两个请求。

在wsdl中检查您的请求消息格式是否将请求作为您在代码中定义的字符串列表?

对于列表中的每个字符串,重复以下代码

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("urlofmy.asmx");

req.Headers.Add(“SOAPAction”,“URLSOAPACTION”); req.ContentType =“text / xml; charset = \”utf-8 \“”; req.Accept =“text / xml”; req.Method =“POST”;

            using (Stream stm = req.GetRequestStream())
            {

                using (StreamWriter stmw = new StreamWriter(stm))
                {
                     foreach (string gamme in listGamme)
                        {

                             stmw.Write(gamme);

                         }   

                 }

            }

WebResponse response = req.GetResponse();
    StreamReader srreader = new StreamReader(response.GetResponseStream());

此外,您将获得每个xml请求的多个响应。

还有一件事,你可以从wsdl粘贴你的请求消息结构吗?也许你可以在单个请求消息中发送多个rquest(取决于你的wsdl请求消息结构)

编辑代码:

string sb = "";
List<string> listGamme = new List<string>();
sb = ("<?xml....") ;//1st listGamme
listGamme.Add(sb);
sb = ("<?xml..."); //2nd listGamme
listGamme.Add(sb);

foreach (string gamme in listGamme)
                        {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("urlofmy.asmx");
    req.Headers.Add("SOAPAction", "URLSOAPACTION");
    req.ContentType = "text/xml;charset=\"utf-8\"";
    req.Accept = "text/xml";
    req.Method = "POST";


            using (Stream stm = req.GetRequestStream())
            {

                using (StreamWriter stmw = new StreamWriter(stm))
                {

                             stmw.Write(gamme); 

                 }

            }

    WebResponse response = req.GetResponse();
    StreamReader srreader = new StreamReader(response.GetResponseStream());
}