2016-05-03 5 views
0

Ich rufe Service dynamisch in URL.my Klasse zu nennen ist Arbeit.aber wenn send Variable mit leerem Wert dann im Dienst bekomme ich für diese Variable Wert "\ n". meiner Klasse Rufbereitschaft ist:Anrufdienst mit URL-Fehler?

public class MyServiceDynamic 
{ 
    public static string CallWebService(string ServiceURL,string ServiceOPname,List<SOAPDataClass> 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<SOAPDataClass> Parameters) 
    { 
     XmlDocument soapEnvelop = new XmlDocument(); 
     string xml = string.Concat("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><", OPname, " xmlns=\"http://tempuri.org/\">"); 
     for (int i = 0; i < Parameters.Count; i++) 
     { 
      xml = string.Concat(xml, "<", Parameters[i].Properties, ">", Parameters[i].Value == "" ? null : Parameters[i].Value, "</", Parameters[i].Properties, ">"); 
     } 
     xml = string.Concat(xml, "</", OPname, "></soap12:Body></soap12:Envelope>"); ; 
     soapEnvelop.LoadXml(xml); 
     return soapEnvelop; 
    } 

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) 
    { 
     using (Stream stream = webRequest.GetRequestStream()) 
     { 
      soapEnvelopeXml.Save(stream); 
     } 
    } 
} 

Klasse Seife Wert Krater ist:

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

Service-Code ist:

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

I Service erfolgreich mit Code aufrufen:

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

wh de Variable s ist leer, Wert der Variable s im Service ist: enter image description here

Wie wird es repariert?

+0

Ich verstehe das Problem nicht angegeben. Könnten Sie bitte Ihre Frage aktualisieren? Könnten Sie bitte auch die vollständige SOAP-Anfrage posten, die gesendet wird? –

Antwort

0

fix CreateSoapEnvelope Code:

private static XmlDocument CreateSoapEnvelope(string OPname, List<SOAPDataClass> Parameters) 
    { 
     XmlDocument soapEnvelop = new XmlDocument(); 
     string xml = string.Concat("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><", 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, ">", @"<![CDATA[", @Parameters[i].Value, "]]>", "</", Parameters[i].Properties, ">"); 
       else 
        xml = string.Concat(xml, "<", Parameters[i].Properties, ">", @Parameters[i].Value, "</", Parameters[i].Properties, ">"); 
      } 
      else 
       xml = string.Concat(xml, "<", Parameters[i].Properties, "/>"); 

     } 
     xml = string.Concat(xml, "</", OPname, "></soap12:Body></soap12:Envelope>"); ; 
     soapEnvelop.LoadXml(xml); 
     return soapEnvelop; 
    }