URL错误的呼叫服务?

我在URL.my类中调用service dynamic来调用是work.but当send变量为空值时,然后在服务中我得到这个变量值“\ n”。 我的class级电话服务是:

public class MyServiceDynamic { public static string CallWebService(string ServiceURL,string ServiceOPname,List Parameters) { XmlDocument soapEnvelopeXml = CreateSoapEnvelope(ServiceOPname,Parameters); HttpWebRequest webRequest = CreateWebRequest(ServiceURL, ServiceURL + "?op=" + ServiceOPname); InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); // begin async call to web request. IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); // suspend this thread until call is complete. You might want to // do something usefull here like update your UI. asyncResult.AsyncWaitHandle.WaitOne(); //asyncResult.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; // get the response from the completed web request. string soapResult; using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) { using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { soapResult = rd.ReadToEnd(); } //Console.Write(soapResult); } BLLBase bb=new BLLBase(); XmlDocument Doc = new XmlDocument(); Doc.LoadXml(soapResult); string result = Doc.GetElementsByTagName(ServiceOPname + "Result")[0].InnerText; if (result.Substring(0, 1) == "[") return result; else if (bb.IsNumeric(result)) return result; else throw new Exception(result); } private static HttpWebRequest CreateWebRequest(string url, string action) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Headers.Add("SOAPAction", action); webRequest.ContentType = "application/soap+xml; charset=utf-8"; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } private static XmlDocument CreateSoapEnvelope(string OPname, List Parameters) { XmlDocument soapEnvelop = new XmlDocument(); string xml = string.Concat(""); for (int i = 0; i < Parameters.Count; i++) { xml = string.Concat(xml, "", Parameters[i].Value == "" ? null : Parameters[i].Value, ""); } xml = string.Concat(xml, ""); ; soapEnvelop.LoadXml(xml); return soapEnvelop; } private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } } } 

class soap value crater是:

 public class SOAPDataClass { public string Properties; public string Value; } 

服务代码是:

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] public string HelloWorld(string s) { return s; } 

我用代码成功调用服务:

 public string test() { List parameters = new List(); parameters.Add(new SOAPDataClass() { Properties = "s", Value = "" }); return MyServiceDynamic.CallWebService("http://myservice.com/service.asmx", "HelloWorld", parameters); } 

当Variable s为空时,服务中Variable s的值为: 在此处输入图像描述

怎么解决?

修复CreateSoapEnvelope代码:

 private static XmlDocument CreateSoapEnvelope(string OPname, List Parameters) { XmlDocument soapEnvelop = new XmlDocument(); string xml = string.Concat("<", OPname, " xmlns=\"http://tempuri.org/\">"); for (int i = 0; i < Parameters.Count; i++) { if (!string.IsNullOrEmpty( Parameters[i].Value) ) { if (Parameters[i].Value.Contains("<") || Parameters[i].Value.Contains(">")) xml = string.Concat(xml, "<", Parameters[i].Properties, ">", @"", ""); else xml = string.Concat(xml, "<", Parameters[i].Properties, ">", @Parameters[i].Value, ""); } else xml = string.Concat(xml, "<", Parameters[i].Properties, "/>"); } xml = string.Concat(xml, ""); ; soapEnvelop.LoadXml(xml); return soapEnvelop; }